2016-07-14 4 views
2

NSubsituteのReceived()メソッドに問題があります。NS substituteとAutoFixtureの問題が受信しました

私のテストクラス:

private readonly IFixture _fixture; 

    public NotificationsCenterTests() 
    { 
     _fixture = new Fixture(); 
     _fixture.Behaviors.Add(new OmitOnRecursionBehavior()); 
     _fixture.Customize(new AutoNSubstituteCustomization()); 
    } 

この方法は、良い作品:

[Theory, AutoNSubstituteData] 
    public void Send_NotificationIsNotNull_NotificationShouldBeSendAndSaved(
     IDocumentDbRepository<NotificationDocument> repository, 
     Notification notification 
     ) 
    { 
     // Arrange 
     var sender = Substitute.For<INotificationSender>(); 

     var notificationsCenter = new NotificationsCenter(
      sender, repository); 
     // Act 
     Func<Task> action = async() => await notificationsCenter.SendAsync(notification); 

     // Assert 
     action.Invoke(); 
     sender.Received(1).Send(Arg.Any<Notification>()); 
    } 

そして、これはエラーを送信します。

[Theory, AutoNSubstituteData] 
    public void Send_NotificationIsNotNull_NotificationShouldBeSendAndSaved2(
     INotificationSender sender, 
     IDocumentDbRepository<NotificationDocument> repository, 
     NotificationsCenter notificationsCenter, 
     Notification notification 
     ) 
    { 
     // Act 
     Func<Task> action = async() => await notificationsCenter.SendAsync(notification); 

     // Assert 
     action.Invoke(); 
     sender.Received(1).Send(Arg.Any<Notification>()); 
    } 

は、それは私のautonsubsisute属性:

internal class AutoNSubstituteDataAttribute : AutoDataAttribute 
{ 
    public AutoNSubstituteDataAttribute() 
     : base(new Fixture() 
      .Customize(new AutoNSubstituteCustomization())) 
    { 
    } 
} 

そして、方法2に誤りがある:

NSubstitute.Exceptions.ReceivedCallsException : Expected to receive exactly 1 call matching: 
    Send(any Notification) 
Actually received no matching calls. 

何が起こっていますか?私はTDDでいくつかのコードをしたいが、私はこの小さな問題で止まった。そして、私は2番目のコードに何が間違っているのか分からない。

あなたは考えがありますか?第2の例で

答えて

2

は、NotificationsCenterrepositorysenderため異なるインスタンスを使用して作成されます。

repositorysenderは、NotificationsCenter引数の前に宣言されていますが、同じインスタンスが再利用されているわけではありません。

あなたは、次のリソースに示すように、このために[Frozen]属性を使用する必要があります。

関連する問題