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

标签 ruby cucumber

如果您认为这是 2 年前提出的重复问题,我首先表示歉意。但我尝试了来自 stackoverflow 的各种答案或在 stackoverflow 之外用谷歌搜索,但它对我不起作用。

我的问题与现有问题几乎相同:未初始化常量 (NameError) 问题 - 如何包含类? Uninitialized constant (NameError) problem - how to include a class?

我的目录结构是:

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'

在我的 domain_layer.rb 中,我写道:

module Domain_Layer
class Account
    attr_accessor :accountid, :service_address, :full_name, :telephone, :email

    def initialize(accountid)
      @accountid = accountid
    end

  end #class

class MyAccount_Web < Account
    def account_number(id)
      return @accountid = id
    end
end #class
end #module

在步骤定义文件中,我写道:

Given(/^the account (\d+) has one to five previous bills$/) do |accountid|
  @web_account = MyAccount_Web.new(accountid)
end

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

cucumber -verbose

我失败了:

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。简单的 require "domain_layer" 应该足够了,因为 lib 文件夹会自动添加到加载路径。

关于ruby - cucumber 中未初始化的常量 (NameError) - 如何将类包含到 _steps.rb?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20057863/

相关文章:

ruby-on-rails - 如何在现有范围之间重新排序

ruby - 使用 watir-webdriver 在 Internet Explorer 上设置 cookie

ruby-on-rails-3 - NoMethodError 异常 : undefined method `use_transactional_fixtures with cucumber selenium

xpath - 在 cucumber 测试中检查链接的正确方法是什么?

java - Cucumber HTML 报告输出出现意外格式

ruby - Rails3 : Functional tests fail with NoMethodError: undefined method `user' for nil:NilClass

ruby - 控制多机 Vagrant block 执行顺序

ruby-on-rails - 仅在某些页面上使用 rails_xss

ruby - 重开Ruby嵌套模块异常

ruby - 多次运行 cucumber 功能