简体   繁体   English

如何在黄瓜中调用方法-步骤定义

[英]How to call a Method in a Cucumber - Step Definition

I'm a newbie to Cucumber framework. 我是Cucumber框架的新手。 I'm trying to call a Ruby method inside of a step definition. 我试图在步骤定义内调用Ruby方法。 Here is how I define my method in lib/methods.rb 这是我在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. 运行测试时,出现“未初始化的常量Test_class(NameError)”。 Any ideas? 有任何想法吗? Thanks. 谢谢。

You haven't instantiated the Test_class object. 您尚未实例化Test_class对象。 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. 这是指向文档的链接,该文档解释了initialize方法以及如何使用它在初始化时设置对象状态。

For class (and module) names, the ruby convention is to use CamelCase . 对于类(和模块)名称,ruby约定是使用CamelCase For example, TestClass instead of Test_class 例如,用TestClass代替Test_class

As orde has said, this is down to initialization. 正如orde所说,这取决于初始化。 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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