如何检查黄瓜没有任何变化?

发布于 2024-11-10 14:35:05 字数 376 浏览 0 评论 0原文

我尝试使用黄瓜/小黄瓜(实际上是规范流)测试的业务场景是,在 Web 表单上给出一组输入,我提出请求,并且需要确保(在某些条件下)当结果为返回,特定字段没有改变(在其他条件下,它改变了)。例如,

假设我在数据输入屏幕上 当我选择“不更新 frobnicator”时 我提交表格 并且显示结果 那么 frobnicator 未更新

我该如何编写“frobnicator 未更新”步骤?

一种选择是在“我提交表单”之前运行一个步骤,其内容类似于“我记得 frobnicator 的值”,但这有点垃圾 - 这是实现细节的可怕泄漏。它分散了测试的注意力,这并不是企业所描述的。事实上,每当有人看到这样的台词时,我都必须解释它。

有谁知道如何更好地实现这一点,理想情况下如所写的那样?

The business scenario I'm trying to test with cucumber/gherkin (specflow, actually) is that given a set of inputs on a web form, I make a request, and need to ensure that (under certain conditions), when the result is returned, a particular field hasn't changed (under other condition, it does). E.g.

Given I am on the data entry screen
When I select "do not update frobnicator"
And I submit the form
And the result is displayed
Then the frobnicator is not updated

How would I write the step "the frobnicator is not updated"?

One option is to have a step that runs before "I submit the form" that reads something like "I remember the value of the frobnicator", but that's a bit rubbish - it's a horrible leak of an implementation detail. It distracts from the test, and is not how the business would describe this. In fact, I have to explain such a line any time anyone sees it.

Does anyone have any ideas on how this could be implemented a bit nicer, ideally as written?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

大海や 2024-11-17 14:35:05

我不同意之前的回答。
感觉想要写的小黄瓜文本可能是正确的。
我将稍微修改一下,以便 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

您如何断言结果将取决于您的程序如何更新 frobnicator,以及为您提供哪些选项。但为了表明这是可能的,我假设您已将数据访问层与您的 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);
}

这里要注意的重要一点是,给定的步骤设置了我们正在测试的对象及其所需的所有内容...我们不需要单独的笨重步骤来说“并且我有一个 frobnicator,我“我要记住”——这对利益相关者来说是不利的,也不利于您的代码灵活性。

[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);
}

根据您的情况调整安排、行动、断言的原则。

I disagree with the previous answer.
The gherkin text you felt like you wanted to write is probably right.
I'm going to modify it just a little to make it so that the When step is the specific action that is being tested.

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

How exactly you Assert the result will depend on how your program updates the frobnicator, and what options that gives you.. but to show it is possible, I'll assume you have decoupled your data access layer from your UI and are able to mock it - and therefore monitor updates.

The mock syntax I am using is from 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);
}

The important thing to note here, is that the given step sets up the object we are testing with ALL the things it needs... We didn't need a separate clunky step to say "and i have a frobnicator that i'm going to memorise" - that would be bad for the stakeholders and bad for your code flexibility.

[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);
}

Adapt the principle of Arrange, Act, Assert depending on your circumstances.

审判长 2024-11-17 14:35:05

考虑一下如何手动测试它:

Given I am on the data entry screen
And the blah is set to "foo"
When I set the blah to "bar"
And I select "do not update frobnicator"
And I submit the form
Then the blah should be "foo"

Think about how you would test it manually:

Given I am on the data entry screen
And the blah is set to "foo"
When I set the blah to "bar"
And I select "do not update frobnicator"
And I submit the form
Then the blah should be "foo"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文