1

I have this situation:

[Given(@"I select cell (.+)")]
[When(@"I select cell (.+)")]
[Then(@"I select cell (.+)")]
public void WhenIClickOnExcelCellX(string cell)
{
     excelDriver.SelectCell(cell);
}

Is there any wildcard attribute which will match any of those three keywords? I'd like to write something like this and not to worry whether I have provided mapping for that attribute or not.

[Any(@"I select cell (.+)")]
public void WhenIClickOnExcelCellX(string cell)
{
    excelDriver.SelectCell(cell);
}

2 Answers 2

5

Actually I'm pretty sure there isn't and that this is by design.

Take a moment and consider what Given, When and Then steps are trying to achieve, I think this is:

  • Given - is a precondition so you don't really care how the code under test gets in that state, just that the state is there so we can run a test.
  • When - is the performing of an action that changes state so we can test it
  • Then - is the check that something has actually got into the correct state

So at most you might consider that if your When I select cell x is a fairly lightweight implementation, you could (but not necessarily should) reuse that for Given I select cell x.

However your Then I select cell x really isn't valid, instead it really should be Then cell x should be selected, i.e.

using Should;
[Then(@"cell (.+) should be selected")] //Regex might need changing
public void ThenCellXShouldBeSelected(string cell)
{
     excelDriver.IsSelected(cell).ShouldBeTrue(); //Or whatever the call is
}

Hope this helps.

Update

Looking at the code at https://github.com/techtalk/SpecFlow/blob/master/Runtime/Attributes.cs reveals that there is a base class StepDefinitionBaseAttribute but it is abstract.

3
  • I agree with you that 'Then I select cell x' isn't really valid. But as you mentioned I can have When and Given keywords for same steps. I'm not trying to avoid it feature file, I think it should be there since it is feature description. But in step definition, I cannot see any pros for this design. It is just implementation stuff and from conceptual level it really has no value since it is not visible.
    – Vajda
    Jun 24, 2013 at 14:38
  • Actually, while it doesn't apply for the example that we have here, the problems arise when your When has side effects, i.e. causes significant operation to occur. For example, if you consider something like, a web operation, then a When I download from URL would be very different to a Given a download from URL. I think the SpecFlow interfaces simply guides you towards that separation.
    – AlSki
    Jun 24, 2013 at 15:34
  • in addition, there's not really any downside to being explicit in putting more than one attribute on the step definition implementation. the absence of some of the attributes or putting on more than one attribute (assuming you actually intend to use the method as either a given or a when) enhances readability by telling the reader about your intent.
    – Dave Rael
    Jul 15, 2013 at 13:11
1

I use 'StepDefinition' attribute for such cases. Though using it will most certainly mean that you are breaking the BDD design philosophy.

[StepDefinition(@"I select cell (.+)")]
public void WhenIClickOnExcelCellX(string cell)
{
    excelDriver.SelectCell(cell);
}
1
  • As far as I see it, this is the answer.
    – pfernandez
    May 26, 2015 at 13:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.