Прошу прощения, если вы чувствуете, что это дублированный вопрос, поднятый 2 года назад. Но я пробовал всевозможные ответы из stackoverflow или googled вне stackoverflow, это просто не работает для меня.Неинициализированная константа (NameError) в огурце - как включить класс в _steps.rb?
Мой вопрос почти такой же, как и существующий: проблема без инициализации константы (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)
С выходом огурцом, я могу видеть огурец просто не проходит через папки, заявленные в env.rb, игнорируя весь файл .rb в каталоге lib, игнорируя требуемый файл domain_layer.rb в каталоге lib.
Я попытался скопировать все .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)
Моя среда рубин 1.8.7, огурец 1.2.1. Я полагаю, что я сделал все, что мог, против всего результата запроса в Интернете, это проблема с версией/средой, или мой код неправильный?
Благодаря
Heads up .. вы, вероятно, получите флэку для использования подчеркивания в имени класса. Это нахмурилось в рубине. – screenmutt
Благодарю вас, ребята, за отзывы. Я просмотрел свой доменный уровень, обнаружил, что поместил класс Account и MyAccount_Web в модуль. Со ссылкой на комментарий Mechanicalfish, я удалил закрытие «module-end», оставив только «class-end» в domain_layer.rb, он просто работает сразу. – user2843204