NUnit与Rhino Mocks异常:为什么抛出此异常?

时间:2010-07-14 16:02:36

标签: nunit rhino-mocks nunit-console rhino-mocks-3.5 nunit-mocks

我得到了一个对我来说毫无意义的异常。

我有一个Expect调用方法,它接受3个参数:类型称为CallContext,IDal和List。

NUnit引发了两个例外:一个是不期望在类型是CallContext,System.Object和List的情况下发生的方法调用,另一个是期望在类型是正确类型的情况下没有发生的调用。有趣的是,调用该方法的唯一方式是上面提到的3种类型。没有类型对象的方法调用!

以下是代码:

 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);
}

调用的方法是:workbooksLogic.Create(context,dal,new List {workbook}) 这是NUnit错误:

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.

我不知道这是怎么回事。有人有什么想法吗?

1 个答案:

答案 0 :(得分:4)

Rhino Mocks使用重载的Equals方法来比较预期调用的参数和实际发生的调用。您作为参数提供的一些对象没有Equals重载(即List类,不确定其他对象),因此如果提供的参数具有相同的引用(因此是相同的对象),它将起作用的唯一方式那些你用来设定期望的人。

您有几个选择:

  1. 使用IgnoreArguments,以便不会检查参数
  2. 提供您自己的约束,以便您可以检查参数是否符合您的预期,但不使用Equals()
  3. 确保这些对象完全相同(如果可能)