Igor Diamandi
Igor Diamandi

Reputation: 11

How can I use When step after a Then step in specflow?

I'd like using specflow for a system test Test steps whould be like:

When I'm selecting "A"
Then "A" item(s) appear
When I'm selecting "B"
Then "A" and "B" item(s) appear
When I'm unselecting "A"
Then "A" item(s) appear

Problem is that 2'nd When is considered as a new method by the specflow. Did anyone of you know what's the solution for that?

Thanks in advance!

Upvotes: 1

Views: 1059

Answers (1)

Sam Holder
Sam Holder

Reputation: 32946

your scenario has strange use of language for me. It implies that you are in the process of doing something, rather then performing and completing and action. I think the When I select 'A' would read better.

Anyway these step definitions should allow your steps to be reused:

[When(@"I'm selecting ""(.*)""")]
public void WhenIMSelecting(string p0)
{
    ScenarioContext.Current.Pending();
}

[Then(@"""(.*)"" item\(s\) appear")]
public void ThenItemSAppear(string p0)
{
    ScenarioContext.Current.Pending();
}

[Then(@"""(.*)"" and ""(.*)"" item\(s\) appear")]
public void ThenAndItemSAppear(string p0, string p1)
{
    ScenarioContext.Current.Pending();
}

[When(@"I'm unselecting ""(.*)""")]
public void WhenIMUnselecting(string p0)
{
    ScenarioContext.Current.Pending();
}

Generally I prefer single quotes to wrap the parameters as it makes the regexes easier to work with, so I would rewrite the scenarios like this:

When I select 'A'
Then 'A' item(s) are shown
When I select 'B'
Then 'A' and 'B' item(s) are shown
When I deselect 'A'
Then 'A' item(s) are shown

Which would result in these step definitions:

[When(@"I select '(.*)'")]
public void WhenISelect(string p0)
{
    ScenarioContext.Current.Pending();
}

[Then(@"'(.*)' item\(s\) are shown")]
public void ThenItemSAreShown(string p0)
{
    ScenarioContext.Current.Pending();
}

[Then(@"'(.*)' and '(.*)' item\(s\) are shown")]
public void ThenAndItemSAreShown(string p0, string p1)
{
    ScenarioContext.Current.Pending();
}

[When(@"I deselect '(.*)'")]
public void WhenIDeselect(string p0)
{
    ScenarioContext.Current.Pending();
}

But obviously its your domain so use whatever language you want in your scenarios :-)

Upvotes: 1

Related Questions