Dry up your code: Simple OR clause [1st mile]
1. Simple OR clause
Consider the following scenario,
#1 - Given the "user" launches the app
#2 - When he logs in
#3 - Then he sees the home page
Lets look at step #2 closer and identify possible variations of it
#2.0 - When he logs in [Def #1: When /^he logs in$/]
#2.1 - When "user" logs in [Def #2: When /^"([^"]*)" logs in$/]
#2.2 - When "admin" logs in [Def #2: When /^"([^"]*)" logs in$/]
#2.3 - When she logs in [Def #3: When /^she logs in$/]
Believe me variation #2.3 will be used a lot if your team has a healthy gender ratio.
Now do we need 3 step definitions to capture all the 4 variations. Nope (otherwise why am I writing this blog anyways :-)
Step: When /^(?:he|she|"([^"]*)") logs in$/
The above definition will hold good for step 2 and all its variations 2.1..2.3
Similarly, following 8 possible variations of step #3,
#3.0 - Then he sees the home page
#3.1 - Then he still sees the home page
#3.2 - Then she sees the home page
#3.3 - Then she still sees the home page
#3.4- Then admin sees the home page
#3.5 - Then admin still sees the home page
#3.6 - Then user sees the home page
#3.7 - Then user still sees the home page
can be defined as,
Step: Then /^(?:he|she|"([^"]*)") (?:sees|still sees) the home page$/
Remember, the Simple OR clause just enables paraphrasing without adding any new parameter to the step definition. No wonder we call it Simple OR clause.
More miles To be treaded...
Comments
Post a Comment