Tutorial :NUnit with Rhino Mocks exception: Why is it throwing this exception?


I'm getting an exception that really makes no sense to me whatsoever.

I have an Expect call for a method that takes 3 arguments into it: The types are called CallContext, IDal, and List.

NUnit throws me 2 exceptions: One for not expecting a method call that happened where the types are CallContext, System.Object, and List, and one for expecting a call that didn't happen where the types are the correct ones. The fun thing is that the only way to call the method is with the 3 types mentioned above. There is no method call with type object!

Here is the code:

 private IDal mockDal;   private CallContext mockContext;   private IWorkbooksLogic mockWLogic;   private ICommercialSpaceLogic mockCLogic;   private CmWorkbook mockWorkbook;   private IList<Workbook> mockList;   private MockRepository mock;  private Random random;     [SetUp]   public void Setup() {     mock = new MockRepository();     random = new Random();     this.mockDal = mock.StrictMock<IDal>() as IDal;     this.mockContext = new CallContext();     this.mockWLogic = mock.StrictMock<IWorkbooksLogic>() as IWorkbooksLogic;     this.mockCLogic = mock.StrictMock<ICommercialSpaceLogic>() as ICommercialSpaceLogic;     this.mockWorkbook = new CmWorkbook();     this.mockList = mock.StrictMock<IList<Workbook>>() as IList<Workbook>;   }    [Test]  public void ShouldFailWhenCreateWorkbookFails() {      int randBudget = random.Next(50);    int randEntity = random.Next(50);    int randWork = random.Next(50);      WorkbookDefinitions work = new WorkbookDefinitions {      WorkbookDefinitionID = randWork    };      Budget budget = new Budget {      BudgetID = randBudget,      WorkbookDefinitions = new List<WorkbookDefinitions> { work },           };      CommercialProperty property = new CommercialProperty {      CommercialPropertyID = randEntity,      CMEntity = new CMEntity {         EntityBase = new EntityEntity { EntityCode = "random.Next(50)" }      }    };      CmWorkbook book = new CmWorkbook {      WorkbookName = String.Format("CM â€" {0}", property.CMEntity.EntityBase.EntityCode)    };      OperationResults results = new OperationResults();    this.mockList.Add(book);    using (mock.Record()) {      Expect.On(this.mockDal).Call(this.mockDal.GetObject<Budget, int>(randBudget)).Return(budget);      Expect.On(this.mockDal).Call(this.mockDal.GetObject<CommercialProperty, int>(randEntity)).Return(property);      Expect.On(this.mockWLogic).Call(this.mockWLogic.Create(this.mockContext, this.mockDal, this.mockList)).Return(null);    }    using (mock.Playback()) {      results = CmWorkbookLogic.CreateWorkbook(mockContext, mockDal, mockWLogic, mockCLogic, randBudget, randEntity);    }    Assert.IsFalse(results.AllSuccessful);  }  

The method being called is: workbooksLogic.Create(context, dal, new List { workbook }) Here is the NUnit error:

ShouldFailWhenCreateWorkbookFails:  Rhino.Mocks.Exceptions.ExpectationViolationException : ICRUDBaseLogic`1.Create(CallContext, System.Object, System.Collections.Generic.List`1[Workbook]); Expected #0, Actual #1.  ICRUDBaseLogic`1.Create(CallContext, IDalProxy8768e63f86da4601993b4791c696ada6, System.Collections.Generic.List`1[Workbook]); Expected #1, Actual #0.  

I have no idea what the heck is going on with this. Anyone have any ideas?


Rhino Mocks uses the overloaded Equals method to compare arguments of the expected invocation and the invocation that actually happened. Some of the objects you are supplying as arguments don't have Equals overloaded (i.e. List class, not sure about the others), so the only way it would work if the supplied arguments had the same references (so were the same objects) as the ones you used to set up the expectation.

You have a few options:

  1. Use IgnoreArguments, so that arguments will not be checked at all
  2. Provide your own constraints, so that you can check if the arguments are what you expect them to be, but without using Equals()
  3. Make sure these are exactly the same objects (if possible)

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »