How to switch between should and should_not in one step definition?

27 views
Skip to first unread message

Chris Bailey

unread,
Jul 21, 2009, 6:30:53 PM7/21/09
to Cukes
I often have the need to write steps that are simply the opposite of
each other, like:

Then there should be an event logged
Then there should not be an event logged

I'd like to be able to implement this as a single step definition and
simply use "should" or "should_not" in my matcher use based on
whichever was specified. I know I can write the step definition as:

Then /^there should( not)? be an event logged$/ do |should_not|

or similar, but the question is then how do you implement it and stay
dry, so that you aren't doing this kind of thing:

if shoudl_not
foo.should_not =~ /some really long regex for example/
else
foo.should =~ /that same regex as above/
end

Obviously the duplication of the regex isn't really the issue (I can
define that once above), but the whole conditional if/else to do this
just feels non-Ruby - I feel like I'm missing some obvious way to
implement this in a nicer way.

aslak hellesoy

unread,
Jul 21, 2009, 6:34:47 PM7/21/09
to cu...@googlegroups.com

I often have the need to write steps that are simply the opposite of
each other, like:

Christopher Bailey

unread,
Jul 21, 2009, 6:46:03 PM7/21/09
to cu...@googlegroups.com
Awesome, thanks!  Man, I had searched the group, but obviously wasn't using the right keywords :) 
--
Christopher Bailey
Cobalt Edge LLC
http://cobaltedge.com

aslak hellesoy

unread,
Jul 21, 2009, 6:48:09 PM7/21/09
to cu...@googlegroups.com
Awesome, thanks!  Man, I had searched the group, but obviously wasn't using the right keywords :) 

It's a hard search to do if you didn't know that thread existed ;-)

Aslak
 
Reply all
Reply to author
Forward
0 new messages