【发布时间】:2016-07-14 12:21:47
【问题描述】:
我对 NSubsitute 的 Received() 方法有疑问。
我的测试课:
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 做一些代码,但我已经停止了这个小问题。而且我不知道第二个代码有什么问题。
你有什么想法吗?
【问题讨论】:
标签: c# unit-testing autofixture nsubstitute