私は前の答えに同意しません。あなたが書きたいと思ったガーキンテキストはおそらく正しいでしょう。ステップがテストされる特定のアクションになるように、少しだけ変更します。When
Given I am on the data entry screen
And I have selected "do not update frobnicator"
When I submit the form
Then the frobnicator is not updated
結果をどの程度正確にアサートするかは、プログラムがフロブニケーターを更新する方法と、提供するオプションによって異なります..しかし、それが可能であることを示すために、UIからデータアクセスレイヤーを切り離し、モックできると仮定しますそれ - したがって、更新を監視します。
私が使用しているモック構文は Moq.
...
private DataEntryScreen _testee;
[Given(@"I am on the data entry screen")]
public void SetUpDataEntryScreen()
{
var dataService = new Mock<IDataAccessLayer>();
var frobby = new Mock<IFrobnicator>();
dataService.Setup(x => x.SaveRecord(It.IsAny<IFrobnicator>())).Verifiable();
ScenarioContext.Current.Set(dataService, "mockDataService");
_testee = new DataEntryScreen(dataService.Object, frobby.Object);
}
ここで注意すべき重要なことは、指定されたステップによって、テスト対象のオブジェクトが必要なものすべてでセットアップされるということです...別の面倒なステップは必要ありませんでした。それは利害関係者にとってもコードの柔軟性にとっても悪いことです。
[Given(@"I have selected ""do not update frobnicator""")]
public void FrobnicatorUpdateIsSwitchedOff()
{
_testee.Settings.FrobnicatorUpdate = false;
}
[When(@"I submit the form")]
public void Submit()
{
_testee.Submit();
}
[Then(@"the frobnicator is not updated")]
public void CheckFrobnicatorUpdates()
{
var dataService = ScenarioContext.Current.Get<Mock<IDataAccessLayer>>("mockDataService");
dataService.Verify(x => x.SaveRecord(It.IsAny<IFrobnicator>()), Times.Never);
}
状況に応じてアレンジ、アクト、アサートの原則を適応させます。