Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does Cucumber raise AmbiguousStepDefinitionsException?

cucumber.runtime.AmbiguousStepDefinitionsException: ✽.Given I am an admin user(src/test/resources/features/alerts.feature:9) matches more than one step definition:
  I am an admin in LoginStepDefinitions.iAmAnAdmin()
  ^I am an admin user$ in AlertsStepDefinitions.iAmAUser()

    at cucumber.runtime.RuntimeGlue.stepDefinitionMatch

I am giving 2 different definitions. I am not sure why this error is coming up.

like image 663
Aruna Rao Avatar asked Dec 15 '22 07:12

Aruna Rao


1 Answers

You're getting this error because

/I am an admin/ # in LoginStepDefinitions.iAmAnAdmin()

and

/^I am an admin user$/ # in AlertsStepDefinitions.iAmAUser()

both match

"I am an admin user" # at src/test/resources/features/alerts.feature:9

The first regexp matches everything that the second regexp does, so any step that matches the second regexp will match both and cause this error.

You can fix it by making the first regexp not a subset of the second regexp. One option is to change the first regexp to

/^I am an admin$/

I generally find it's a good idea to require step definitions to match the entire step (i.e. to begin with ^ and to end with $) until I find a good reason not to. But it's up to you how to organize your steps so that they're all unambiguous and make sense to the reader.

like image 84
Dave Schweisguth Avatar answered May 11 '23 04:05

Dave Schweisguth