收到的 NSubstitute 和 AutoFixture 问题
NSubstitute and AutoFixture issue with Received
我对 NSubsitute 的 Received()
方法有疑问。
我的测试class:
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>());
}
这是我的自动订阅属性:
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 做一些代码,但我已经停在了这个小问题上。而且我不知道第二个代码有什么问题。
你有什么想法吗?
在第二个示例中,NotificationsCenter
是使用 不同的 个实例创建的 repository
和 sender
。
尽管 repository
和 sender
在 NotificationsCenter
参数之前声明,但这并不意味着重复使用相同的实例。
您需要为此使用 [Frozen]
属性,如以下资源所示:
我对 NSubsitute 的 Received()
方法有疑问。
我的测试class:
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>());
}
这是我的自动订阅属性:
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 做一些代码,但我已经停在了这个小问题上。而且我不知道第二个代码有什么问题。
你有什么想法吗?
在第二个示例中,NotificationsCenter
是使用 不同的 个实例创建的 repository
和 sender
。
尽管 repository
和 sender
在 NotificationsCenter
参数之前声明,但这并不意味着重复使用相同的实例。
您需要为此使用 [Frozen]
属性,如以下资源所示: