Dry up your code: Decisive OR clause [2nd mile]
2. Decisive OR clause
Lets take step #3 of the same scenario:
#1 - Given the "user" launches the app
#2 - When he logs in
#3 - Then he sees the home page
Possible (logical) variations of step #3 could be
#3.0 - Then he sees the home page
Def #1: Then /^he sees the home page$/ do
@home_page.should be_displayed
end
#3.1 - Then he does not see the home page
Def #2: Then /^he does not see the home page$/ do
@home_page.should_not be_displayed
end
Now lets use the power of Decisive OR clause to define the step as
Def: Then /^he (sees|does not see) the home page$/ do |condition|
condition.include?("not") ? @home_page.should_not be_displayed
: @home_page.should be_displayed
end
To further understand lets go into the questions mode,
What do we have here ?
Ability to decide the behavior of the step dynamically.
Ability to decide the behavior of the step dynamically.
How did we get this ability ?
We used decisive OR which gives us a hold of what the user says, which is the condition parameter in the above step. This parameter is then leveraged to decide the intended behavior.
We used decisive OR which gives us a hold of what the user says, which is the condition parameter in the above step. This parameter is then leveraged to decide the intended behavior.
Decisive ?
Yes, if you look at the OR clause closely it is (sees|does not see)which is not a Simple OR that has ?: (?:sees|does not see) . So its not just paraphrasing, instead, we get a parameter which lets us to decide the behavior of the step.
Yes, if you look at the OR clause closely it is (sees|does not see)which is not a Simple OR that has ?: (
More miles To be treaded...
Comments
Post a Comment