2
我對NSubsitute的Received()
方法有問題。收到的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>());
}
據我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做一些代碼,但是我已經停止了這個小問題。我不知道第二個代碼有什麼問題。
你有什麼想法嗎?