2

ユニットテストでこのようなものがあります

public class MyTestClass()
{
      private Mock<IAccountRepo> accountRepo;
       private AdminService adminService;

      [Setup]
      public void Setup()
      {
        accountRepo = fixture.Freeze<Mock<IAccountRepo>>();
        adminService = fixture.CreateAnonymous<AdminService>();
      }

      [Test]
      public Test()
      {
          accountRepo.Setup(x => x.Insert(It.IsAny<IUnitOfWork>(), It.IsAny<MyDomainObject>()));

            adminService.ApplyAdminFee(1, 1, today);

            accountRepo.Verify(x => x.Insert(It.IsAny<IUnitOfWork>(), It.Is<MyDomainObject>(a => a.Id == 1)));
      }
}

このエラーが発生します。

System.NullReferenceException was unhandled by user code
  Message=Object reference not set to an instance of an object.
  Source=Anonymously Hosted DynamicMethods Assembly
  StackTrace:
       at lambda_method(Closure , MyDomainObject )
       at Moq.It.<>c__DisplayClass2`1.<Is>b__1(TValue value)
       at Moq.Match`1.Matches(Object value)
       at Moq.Matcher.Matches(Object value)
       at Moq.MethodCall.Matches(IInvocation call)
       at Moq.Mock.<>c__DisplayClassc.<VerifyCalls>b__b(IInvocation i)
       at System.Linq.Enumerable.WhereListIterator`1.MoveNext()
       at System.Linq.Enumerable.Count[TSource](IEnumerable`1 source)
       at Moq.Mock.VerifyCalls(Interceptor targetInterceptor, MethodCall expected, Expression expression, Times times)
       at Moq.Mock.Verify[T](Mock mock, Expression`1 expression, Times times, String failMessage)
       at Moq.Mock`1.Verify(Expression`1 expression, Times times)
       at Test() in 383
  InnerException: 

理由はわかりません。

編集

私は何が起こっているのか知っていると思います。私のメソッドでは、insertメソッドを3回呼び出しています(insertメソッドはオブジェクトを受け取ります)。

だから私は好きです

accountRepo.Insert(MyDomainObject);
accountRepo.Insert(MyOtherDomainObject);
accountRepo.Insert(MyOtherOtherDomainObject);

それで、おそらくそれらの他の2つのインサートがそれをオーバーライドしていますか?どうすればこれを回避できますか?

4

1 に答える 1

5

問題はここにあります: It.Is<MyDomainObject>(a => a.Id == 1). スタックトレースからわかります。

次のように変更します。It.Is<MyDomainObject>(a => a != null && a.Id == 1)

于 2012-11-10T05:05:51.357 に答える