2016-07-14 17 views
2

Ich habe ein Problem mit Received() Methode von NSubsuite.NSubstitute und AutoFixture Problem mit empfangenen

Meine Testklasse:

private readonly IFixture _fixture; 

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

Diese Methode funktioniert gut:

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

Und dies sendet Fehler:

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

Es ist mein autonsubsisute Attribut:

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

Und der Fehler in Methode 2:

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

Was ist los? Ich möchte etwas Code mit TDD machen, aber ich habe bei diesem kleinen Problem aufgehört. Und ich habe keine Ahnung, was mit dem zweiten Code falsch ist.

Haben Sie irgendwelche Gedanken?

Antwort

2

Im zweiten Beispiel wird NotificationsCenter für repository und sender mit verschiedenen Instanzen erstellt.

Obwohl repository und sender vor dem Argument NotificationsCenter deklariert sind, bedeutet dies nicht, dass dieselbe Instanz wiederverwendet wird.

Sie müssen das [Frozen] Attribut für diese verwenden, wie in den folgenden Ressourcen gezeigt: