Ruby cucumber中的未初始化常量(NameError)-如何将类包含到_steps.rb?

Ruby cucumber中的未初始化常量(NameError)-如何将类包含到_steps.rb?,ruby,cucumber,Ruby,Cucumber,如果你觉得这是两年前提出的重复问题,我首先向你道歉。但我尝试了stackoverflow的各种答案,或者在stackoverflow的一边用谷歌搜索,这对我来说根本不起作用 我的问题与现有的问题基本相同:未初始化常量(NameError)问题-如何包含类? 我的目录结构是: myacc/features/account_bill.feature myacc/features/step_definitions/account_bill_steps.rb myacc/features/suppor

如果你觉得这是两年前提出的重复问题,我首先向你道歉。但我尝试了stackoverflow的各种答案,或者在stackoverflow的一边用谷歌搜索,这对我来说根本不起作用

我的问题与现有的问题基本相同:未初始化常量(NameError)问题-如何包含类?

我的目录结构是:

myacc/features/account_bill.feature
myacc/features/step_definitions/account_bill_steps.rb
myacc/features/support/env.rb

myacc/lib/domain_layer.rb
在env.rb中:我提出了多种解决方案

require File.join(File.dirname(__FILE__),'..', '..', 'lib', 'domain_layer')
require File.expand_path(File.dirname(__FILE__) + "/../../lib/domain_layer")

$LOAD_PATH.unshift(File.dirname(__FILE__) + '/../../lib')
$: << File.dirname(__FILE__)+'/../../lib/'

require 'domain_layer'
当我在项目“myacc”的根目录中运行命令行时:

我在以下方面失败了:

C:\myacc01>cucumber -verbose
Code:
  * features/support/env.rb
  * features/step_definitions/account_bill_steps.rb

Features:
  * features/account_bill.feature
  * features/account_bill_1.feature
Parsing feature files took 0m0.027s
...
Given the account 03147102942 has one to five previous bills
    uninitialized constant MyAccount_Web (NameError)
从cucumber的输出中,我可以看到cucumber没有遍历env.rb中声明的文件夹,忽略lib目录中的所有.rb文件,忽略lib目录中所需的domain_layer.rb

我试图将所有.rb文件复制到step_definitions文件夹中,但仍然无法工作:

myacc/features/account_bill.feature
myacc/features/step_definitions/account_bill_steps.rb
myacc/features/step_definitions/domain_layer.rb
myacc/features/support/env.rb

myacc/lib/domain_layer.rb
结果是:

C:\myacc01>cucumber -verbose
Code:
  * features/support/env.rb
  * features/step_definitions/account_bill_steps.rb
  * features/step_definitions/customer_account_bill.rb
  * features/step_definitions/domain_layer.rb
  * features/step_definitions/web_service_layer.rb

Features:
  * features/account_bill.feature
  * features/account_bill_1.feature
Parsing feature files took 0m0.028s
...
Scenario: The account has one to five previous months bills
  Given the account 03147102942 has one to five previous bills
    uninitialized constant MyAccount_Web (NameError)
我的环境是ruby 1.8.7和cucumber 1.2.1。我假设我对互联网上的所有查询结果做了我应该做的一切,是版本/环境问题,还是我的代码错了


谢谢

如果您的类是在模块中定义的,您必须像这样访问它:

Domain_Layer::MyAccount_Web
还要删除类和模块名称中的下划线(这是一种命名约定)


之后,清理
env.rb
。简单的
需要“域\u层”
就足够了,因为lib文件夹会自动添加到加载路径。

请注意。。您可能会因为在类名中使用下划线而受到抨击。这在ruby中是不受欢迎的。非常感谢你们的反馈。我查看了我的域层,发现我把类帐户和我的帐户放在了模块中。根据mechanicalfish的评论,我删除了'module-end'闭包,只在domain_layer.rb中留下'class-end',它马上就可以工作了。没错,谢谢你这么多鱼。我删除了模块结束闭包,删除了带有类名的下划线,现在一切正常!非常感谢!:-)
myacc/features/account_bill.feature
myacc/features/step_definitions/account_bill_steps.rb
myacc/features/step_definitions/domain_layer.rb
myacc/features/support/env.rb

myacc/lib/domain_layer.rb
C:\myacc01>cucumber -verbose
Code:
  * features/support/env.rb
  * features/step_definitions/account_bill_steps.rb
  * features/step_definitions/customer_account_bill.rb
  * features/step_definitions/domain_layer.rb
  * features/step_definitions/web_service_layer.rb

Features:
  * features/account_bill.feature
  * features/account_bill_1.feature
Parsing feature files took 0m0.028s
...
Scenario: The account has one to five previous months bills
  Given the account 03147102942 has one to five previous bills
    uninitialized constant MyAccount_Web (NameError)
Domain_Layer::MyAccount_Web