2016-07-14 2 views
2

У меня проблема с методом Received() от NSubsitute.Проблема с NSubstitute и 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>()); 
    } 

It мой атрибут 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

Во втором примере, NotificationsCenter создаются с различными случаями для repository и sender.

Хотя repository и sender объявлены до аргумента NotificationsCenter, это не означает, что один и тот же экземпляр используется повторно.

Вы должны использовать атрибут [Frozen] для этого, как показано на следующих ресурсах:

Смежные вопросы