4

Lets say i have a scenario in feature file like below

Given I log in as "super" user
When I click on login
Then Home page is displayed

With corresponding step definitions:

[Given(@"I log in as ""(.*)"" user")]
public void GivenIHaveLogInAsUser(string p0)
{
    ScenarioContext.Current.Pending();
}

Now I want to change

Given I log in as "super" user

To

Given I have logged in as "super" user

When I make this change in feature file how to get SpecFlow to make this change automatically in the step definition.

Greg Burghardt
  • 17,900
  • 9
  • 49
  • 92
Dee
  • 41
  • 1
  • 2

2 Answers2

4

UPDATE

This feature was added in a fairly recent update, so you should be able to follow the instructions here, which basically say

You can globally rename steps and update the associated bindings automatically. To do so:

  • Open the feature file containing the step.
  • Right-click on the step you want to rename and select Rename from the context menu.
  • Enter the new text for the step in the dialog and confirm with OK.
  • Your bindings and all feature files containing the step are updated.

Note: If the rename function is not affecting your feature files, you may need to restart Visual Studio to flush the cache.

previous answer

This is not possible I don't believe. you have 2 options:

  • amend the step definition text to match the new text
  • add the new definition to the step as well

like so:

  [Given(@"I log in as ""(.*)"" user")]
  [Given(@"I have logged in as ""(.*)"" user")]  
  public void GivenIHaveLogInAsUser(string p0)
  {
      ScenarioContext.Current.Pending();
  }

This will allow steps with both pieces of text to match

Sam Holder
  • 32,535
  • 13
  • 101
  • 181
  • Thanks a lot Sam for looking at it. Though it solves the problem but what i am looking for is a shortcut or a provision within a specflow which will change corresponding step definition when i actually change the step text. So i do not have to go the step definition. Its something like renaming a variable in C# which will rename everywhere it has been used. Not sure if it is even possible. – Dee Nov 19 '14 at 11:19
  • As I said I don't think its currently possible. but SpecFlow is [open source so please file a pull request](https://github.com/techtalk/SpecFlow) which adds this feature, you are not the only one who wants it. – Sam Holder Nov 19 '14 at 13:24
  • You'll have to resort to a Good 'Ol Fashioned regular expression search and replace. My only complaint about Visual Studio (I'm using 2010) is its lackluster support for Regex's in the search tool. – Greg Burghardt Nov 25 '14 at 21:34
3

Bit late to this thread, but you may use visual studios find and replace tool with regex enabled to replace both the step definition and the step implementations together.

e.g for the step definition: Given I have a message '(.*)' that is (.*) characters long, we can use the step definition itself to use in search for any matching steps, and replace with the new step. $n can be used to carry over regex matches picked up in the find.

Find: I have a message '(.*)' that is (.*) characters long

Replace: I have a message $1 that is $2 characters in length

Result: 'I have a message 'myMessage' that is 100 characters long' becomes 'I have a message 'myMessage' that is 100 characters in length'

chrisc
  • 434
  • 3
  • 9