0

This is related to an earlier question I had regarding cucumber optional parameters. How can I tell within the step whether it's been called with the for email address.. bit?

(I know in my specific circumstance I can check whether email = default@domain.com but is there an args.count or more general way of telling what params a step has been called with?)

Community
  • 1
  • 1
larryq
  • 15,713
  • 38
  • 121
  • 190

1 Answers1

0

I think the only way you can infer that information is by checking the values of the parameters. If the value is nil, nothing was assigned to it (ie that part of the step definition was not used).

For example, if you want behaviour depending on if the email address part was used:

When /^I check the favorite color count(?: for email address (.*))?$/ do |email|
  if email.nil?
    # Then the "for email address" part was *not* used
  else
    # Then the "for email address" part was used
  end

  email ||= "default@domain.com"
  puts 'using ' + email
end

Note that this is basically what the line email ||= "default@domain.com" is doing. The ||= basically means assign this value if one does not already exist. It would be the same as doing:

When /^I check the favorite color count(?: for email address (.*))?$/ do |email|
  if email.nil?
    # Then the "for email address" part was *not* used
    email = "default@domain.com"
  else
    # Then the "for email address" part was used
    # email already has a value so leave it be
  end

  puts 'using ' + email
end
Justin Ko
  • 46,526
  • 5
  • 91
  • 101