简体   繁体   中英

How to call a Method in a Cucumber - Step Definition

I'm a newbie to Cucumber framework. I'm trying to call a Ruby method inside of a step definition. Here is how I define my method in lib/methods.rb

class Test_class

  def create_test_scenario()
   puts "here!!!"
  end

end

This is how I try to call the method inside of a step definition:

 And(/^I create scenarios$/) do
   Test_class.create_test_scenario
 end

I'm getting 'uninitialized constant Test_class (NameError)' when I run the test. Any ideas? Thanks.

You haven't instantiated the Test_class object. For example:

class Test_class
  def create_test_scenario
    puts "here!!!"
  end
end

Test_class.new.create_test_scenario  # notice `new` method chained here
#=> here!!!

Errata:

Here's a link to documentation that explains the initialize method and how you can use it to set up object state on initialization.

For class (and module) names, the ruby convention is to use CamelCase . For example, TestClass instead of Test_class

As orde has said, this is down to initialization. To help put the code into context, you would initialize the class object in your step definition as an instance variable (which starts with @). So it would look like this:

 And(/^I create scenarios$/) do
   @Test_class = Test_class.new
   @Test_class.create_test_scenario
 end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM