0

I am using calabash-android to test my app.

I created my own step:

Then /^There should be (\d+) customers$/ do |nr_of_customers|
 ...
end

after that, I create another step, which needs to call the above existing step, I know I can use macro, so I tried this:

Given /^I hosted (\d+) customers$/ do |nr_of_customers|
 #How to pass the nr_of_customers to the macro???
 macro 'There should be nr_of_customers'
 ...
end

But, how can I pass the parameter nr_of_customers to the macro which calls the other step function?

Leem.fin
  • 40,781
  • 83
  • 202
  • 354

1 Answers1

3

Don't call steps from within steps, you'll end up with a mess of spaghetti code if you do. Instead extract helper methods from your step definitions and call these instead.

e.g.

Then /^There should be (\d+) customers$/ do |nr_of_customers|
 expect(customer_count).to be nr_of_customers
end

Given /^I hosted (\d+) customers$/ do |nr_of_customers|
  # do some stuff to set up the customers
  expect(customer_count).to be nr_of_customers
  ...

module StepHelpers
  def customer_count
    ....

In addition its bad practice to embed then statements in Givens. Givens are about setting up state not testing consequences so really your given should be something like

Given /^I hosted (\d+) customers$/ do |nr_of_customers|
   nr_of_customers.times do
     host_customer
   end

And host_customer should be helper you created when you wrote the scenarios that showed you could host customers.

diabolist
  • 3,990
  • 1
  • 11
  • 15
  • I got `Undefined local variable or method 'customer_count'`, how to invoke the helper function in step definition properly? – Leem.fin Sep 07 '16 at 10:35
  • add your module to your World (assuming calabash-android has that). In Cucumber ruby you would do `module MyStepHelper ...end World MyStepHelper`. This makes the methods available in your steps. – diabolist Sep 08 '16 at 16:50