在 Cucumber 中声明一个全局方法

Declaring a global method in Cucumber

如果我有以下模块

module UserSession
 $user_array = factory_girl_users.values

 def factory_girl_users
  Hash[:user_1 => FactoryGirl.attributes_for(:automated_user), :user_2 => FactoryGirl.attributes_for(:automated_user_1)]
 end
end
World(UserSession)

如何访问 factory_girl_users 方法,因为目前我得到:

undefined method `factory_girl_users' for UserSession:Module (NoMethodError)

我想不出是什么原因造成的。

我想你可以定义一个模块方法

module UserSession

  def self.factory_girl_users
    Hash[:user_1 => FactoryGirl.attributes_for(:automated_user), :user_2 => FactoryGirl.attributes_for(:automated_user_1)]
   end

  def self.user_array
    factory_girl_users.values
  end
end

要访问 user_array 你会做 UserSession.user_array