我有一个任务是学习如何使用隔离框架。我想知道我是否正在使用 nsubstitute 以专有的方式制造假货。这是应用程序应该是什么样子的序列图。
然后我进行了单元测试并提交了练习。有人告诉我,我没有使用隔离框架来制造假货(而不是之前我们自己制造的)。这是我的两个测试的示例:
[Test]
public void RequestEntry_WithValidId_DoorStateIsClosed()
{
var door = new Door();
var alarm = new Alarm();
var userValidation = new UserValidation();
var entryNotifier = Substitute.For<IEntryNotification>();
var uut = new DoorControl(userValidation, entryNotifier, door, alarm);
door.DoorCtrl = uut;
uut.RequestEntry(1);
Assert.That(uut.DState == DoorState.Closed);
entryNotifier.Received(1).EntryNotificationGranted();
}
和
public void RequestEntry_WithValidId_DoorOpens()
{
var door = Substitute.For<IDoor>();
var alarm = new Alarm();
var userValidation = new UserValidation();
var entryNotifier = new EntryNotification();
var uut = new DoorControl(userValidation, entryNotifier, door, alarm);
door.DoorCtrl = uut;
uut.RequestEntry(1);
door.Received(1).Open();
}
所以我知道在这两个测试中都有一些问题是错误的,因为我需要两个存根,所以我对它们进行了一些更改,所以他们知道看起来像这样:
public void RequestEntry_WithValidId_CorrectIdParsedToUserValidation()
{
var door = Substitute.For<IDoor>();
var alarm = new Alarm();
var userValidation = Substitute.For<IUserValidation>();
userValidation.ValidateEntryRequest(Arg.Any<int>()).Returns(true);
var entryNotifier = Substitute.For<IEntryNotification>();
var uut = new DoorControl(userValidation, entryNotifier, door, alarm);
door.DoorCtrl = uut;
uut.RequestEntry(1);
userValidation.Received(1).ValidateEntryRequest(1);
}
和
public void RequestEntry_WithValidId_DoorOpens()
{
var door = Substitute.For<IDoor>();
var alarm = new Alarm();
var userValidation = Substitute.For<IUserValidation>();
userValidation.ValidateEntryRequest(Arg.Any<int>()).Returns(true);
var entryNotifier = Substitute.For<IEntryNotification>();
var uut = new DoorControl(userValidation, entryNotifier, door, alarm);
door.DoorCtrl = uut;
uut.RequestEntry(1);
door.Received(1).Open();
}
我正确使用 nsubstitute 吗?或者我是否还需要添加一些其他的东西(Alarm 类没有在这两个测试用例中使用,但 DoorControl 构造函数的参数需要它,所以我看不到存根它们的理由)?