Indicating Whether a Step Should Continue

11 views
Skip to first unread message

Jeff Nyman

unread,
Feb 13, 2012, 9:32:46 AM2/13/12
to Cukes
Hello all.

I'm struggling with how to do something that I think is probably
really simple. The situation is that I'm in an environment where we
will be creating data on the fly for our tests. (These are browser
tests that are driven by Selenium and I'm using page objects.) So I
allow a statement like this:

Given the default customer exists

If the default customer does not exist, it will be created. Here is a
slightly truncated example of my step definition:

======================================
Given /^the default customer(?: exists)?$/ do
step %{I go to the login page}
step %{I login as "cleartrial administrator"}

customer = customer_data("default")
customer_name = customer["name"]

filter_by(customer_name)
entity = search_table_for(customer_name)

if entity == false and not entity_should_be_created
test_log("Entity creation was not enabled.")
fail #raise TestCannotContinueException
elsif entity == true
## WHAT DO I PUT HERE?
end

on EntityList do |page|
...
end

on CustomerCreate do |page|
...
end
======================================

So what's happening here is that if the entity was not found in the
system and if a flag indicating entities should be created was not
set, then the step is made to fail.

The trick for me is this: what if the entity does exist? In that case,
the creation step does not need to continue since the default customer
is present and thus the Given context is satisfied.

The logic after those checks handles actually creating the customer --
and that would only run if the entity was not found to exist and the
flag to create entitites as needed was set.

So what I don't know is what to put in place of the "WHAT DO I PUT
HERE?" text. I simply want this step to pass at that point.

Any pointers?

Note: I do realize I could just wrap the creation logic in a condition
like this:

======================================
if entity == false
on EntityList do |page|
...
end

on CustomerCreate do |page|
...
end
end
======================================

Maybe that's in fact what I should be doing. But I wanted to see if
there was some way to indicate to a step that it has passed and
execution should continue to the next step.

Any suggestions about my approach are welcomed.

- Jeff

Richard Lawrence

unread,
Feb 13, 2012, 10:05:07 AM2/13/12
to cu...@googlegroups.com
Steps pass if they don't raise an error. You don't have to (and, really, can't) actively do anything to tell Cucumber a step has passed. So, "if the customer already exists, don't do anything" is the right logic here, IMO.

Richard

> --
> You received this message because you are subscribed to the Google Groups "Cukes" group.
> To post to this group, send email to cu...@googlegroups.com (mailto:cu...@googlegroups.com).
> To unsubscribe from this group, send email to cukes+un...@googlegroups.com (mailto:cukes+un...@googlegroups.com).
> For more options, visit this group at http://groups.google.com/group/cukes?hl=en.

Rob Hunter

unread,
Feb 13, 2012, 10:09:12 AM2/13/12
to cu...@googlegroups.com

Jeff,

Are you familiar with the Ruby keyword `return` and its cousin `next`? They stop executing the current method or block in the same way as reaching the end. A safety check at the top of a method is called a "guard clause".

I like to keep my step definitions very thin indeed -- simply a translation layer from a line of English to a line of code wherever possible.

Thus, I might write something like the following:

Given 'the default customer' do
  ensure_customer_exists(default_name)
end

def ensure_customer_exists(name)
  return if customer_exists?(name)
  create_customer(name)
end

--
Rob Hunter
(typed with my thumbs)

> --
> You received this message because you are subscribed to the Google Groups "Cukes" group.

> To post to this group, send email to cu...@googlegroups.com.
> To unsubscribe from this group, send email to cukes+un...@googlegroups.com.

Jeff Nyman

unread,
Feb 13, 2012, 11:02:52 AM2/13/12
to Cukes
Thanks for the help. This was confirming what I was suspecting and it
makes sense. So what I ended up doing was this:

==========================================
if entity == false and not entity_should_be_created
test_log("Entity creation was not enabled.")
raise Exceptions::ContextCannotBeFulfilledException
elsif entity == true
test_log("The customer '#{customer_name}' was found.")
else
create_customer(customer)
end
==========================================

The create_customer() method now contains all that customer creation/
page object logic. This seems to work just fine.

The help is much appreciated.

- Jeff
Reply all
Reply to author
Forward
0 new messages