簡體   English   中英

如何在黃瓜中調用方法-步驟定義

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

我是Cucumber框架的新手。 我試圖在步驟定義內調用Ruby方法。 這是我在lib / methods.rb中定義我的方法的方式

class Test_class

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

end

這是我嘗試在步驟定義內調用方法的方式:

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

運行測試時,出現“未初始化的常量Test_class(NameError)”。 有任何想法嗎? 謝謝。

您尚未實例化Test_class對象。 例如:

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

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

勘誤表:

這是指向文檔的鏈接,該文檔解釋了initialize方法以及如何使用它在初始化時設置對象狀態。

對於類(和模塊)名稱,ruby約定是使用CamelCase 例如,用TestClass代替Test_class

正如orde所說,這取決於初始化。 為了幫助將代碼置於上下文中,您可以將步驟定義中的類對象初始化為實例變量(以@開頭)。 所以它看起來像這樣:

 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