How to get SpecFlow to change step definition when renaming step in feature file?

3.3k views Asked by At

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.

2

There are 2 answers

3
Sam Holder On

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

0
chrisc On

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'