Search code examples
cucumbercalabashgherkincalabash-androidcalabash-ios

call an existing step inside a custom step with parameter


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?


Solution

  • 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.