kernel_require.rb:55:在`require':无法加载此类文件错误

时间:2014-02-17 14:39:16

标签: ruby cucumber

我目前正在使用Ruby版本1.9.3(尽管我在Ruby 2.0.0中遇到了同样的问题)。在Windows 7 64位上。我正在关注'The Cucumber Book'并且陷入第7.2章 - “删除重复变换”。我的文件夹结构如下:

\cash_withdrawal
\cash_withdrawal\Gemfile
\cash_withdrawal\Gemfile.lock
\cash_withdrawal\features
\cash_withdrawal\features\cash-withdrawal.feature
\cash_withdrawal\features\step_definitions
\cash_withdrawal\features\step_definitions\cash_withdrawal_steps.rb
\cash_withdrawal\features\step_definitions\lib
\cash_withdrawal\features\step_definitions\lib\nice_bank.rb
\cash_withdrawal\features\support
\cash_withdrawal\features\support\env.rb
\cash_withdrawal\features\support\transforms.rb
\cash_withdrawal\features\support\world_extensions.rb

在我的cash_withdrawal_steps.rb文件中,我有:

require 'CAPTURE_CASH_AMOUNT'

Given /^I have deposited (#{CAPTURE_CASH_AMOUNT}) in my Account$/ do |amount|
  my_account.deposit(amount)
  my_account.balance.should eq(amount), 
    "Expected the balance to be #{amount} but it was #{my_account.balance}"
end

当我运行cucumber时,我得到:

  

C:\用户\ Nikita.Harrison \ AutomatedTesting \ cash_withdrawal&GT;黄瓜   无法加载此类文件 - CAPTURE_CASH_AMOUNT(LoadError)   C:/Ruby193/lib/ruby/site_ruby/1.9.1/rubygems/core_ext/kernel_require.rb:55:在   r equire' C:/Ruby193/lib/ruby/site_ruby/1.9.1/rubygems/core_ext/kernel_require.rb:55:in 征服'   C:/Users/Nikita.Harrison/AutomatedTesting/cash_withdrawal/features/step_definiti   ons / cash_withdrawal_steps.rb:1:in <top (required)>' C:/Ruby193/lib/ruby/gems/1.9.1/gems/cucumber-1.3.10/lib/cucumber/rb_support/rb_l anguage.rb:122:in load'   C:/Ruby193/lib/ruby/gems/1.9.1/gems/cucumber-1.3.10/lib/cucumber/rb_support/rb_l   anguage.rb:122:in load_code_file' C:/Ruby193/lib/ruby/gems/1.9.1/gems/cucumber-1.3.10/lib/cucumber/runtime/support _code.rb:180:in load_file'C:/Ruby193/lib/ruby/gems/1.9.1/gems/cucumber-1.3.10/lib/cucumber/runtime/support   _code.rb:83:in block in load_files!' C:/Ruby193/lib/ruby/gems/1.9.1/gems/cucumber-1.3.10/lib/cucumber/runtime/support _code.rb:82:in每个'C:/Ruby193/lib/ruby/gems/1.9.1/gems/cucumber-1.3.10/lib/cucumber/runtime/support   _code.rb:82:in load_files!' C:/Ruby193/lib/ruby/gems/1.9.1/gems/cucumber-1.3.10/lib/cucumber/runtime.rb:184: in load_step_definitions'   C:/Ruby193/lib/ruby/gems/1.9.1/gems/cucumber-1.3.10/lib/cucumber/runtime.rb:42:我   n run!' C:/Ruby193/lib/ruby/gems/1.9.1/gems/cucumber-1.3.10/lib/cucumber/cli/main.rb:47: in执行!'   C:/Ruby193/lib/ruby/gems/1.9.1/gems/cucumber-1.3.10/bin/cucumber:13:在   <top (re quired)>' C:/Ruby193/bin/cucumber:23:in负载”   C:/ Ruby193 / bin / cucumber:23:在''

如果我运行irb然后运行require "CAPTURE_CASH_AMOUNT",我会收到此错误:

  

IRB(主):006:0&GT;需要“CAPTURE_CASH_AMOUNT”LoadError:无法加载   这样的文件 - CAPTURE_CASH_AMOUNT           来自C:/Ruby193/lib/ruby/site_ruby/1.9.1/rubygems/core_ext/kernel_requir   e.rb:55:in require' from C:/Ruby193/lib/ruby/site_ruby/1.9.1/rubygems/core_ext/kernel_requir e.rb:55:in要求'           来自(irb):6           来自C:/ Ruby193 / bin / irb:12:在''

我尝试了许多修复程序,包括'require_relative',似乎没有解决我的问题。 如果我从require 'CAPTURE_CASH_AMOUNT'文件中删除cash_withdrawal_steps.rb并运行黄瓜,那么我的“步骤定义”不会按照定义显示:

C:\Users\Nikita.Harrison\AutomatedTesting\cash_withdrawal>cucumber
Feature: Cash withdrawal

   @test   Scenario: Successful withdrawal from an account in credit #
 features\cash_with drawal.feature:4
     Given I have deposited $100 in my Account               # features\cash_with drawal.feature:5
     When I withdraw $20                                     # features/step_defi nitions/cash_withdrawal_steps.rb:7
     Then $20 should be dispensed                            # features/step_defi nitions/cash_withdrawal_steps.rb:11
     And the balance of my account should be $80             # features/step_defi nitions/cash_withdrawal_steps.rb:15

 1 scenario (1 undefined) 4 steps (3 skipped, 1 undefined) 0m0.006s

 You can implement step definitions for undefined steps with these
 snippets:

 Given(/^I have deposited \$(\d+) in my Account$/) do |arg1|   pending
 \# express the regexp above with the code you wish you had end

如果我将require File.join(File.dirname("C:/Users/Nikita.Harrison/AutomatedTesting/cash_withdrawal/features/support"), 'support', 'transforms')添加到env.rb文件并运行cucumber,我会得到:

 C:/Users/Nikita.Harrison/AutomatedTesting/cash_withdrawal/features/support/trans
 forms.rb:1: warning: already initialized constant CAPTURE_CASH_AMOUNT
 Feature: Cash withdrawal

   @test   Scenario: Successful withdrawal from an account in credit #
 features\cash_with drawal.feature:4
     Given I have deposited $100 in my Account               # features\cash_with drawal.feature:5
     When I withdraw $20                                     # features/step_defi nitions/cash_withdrawal_steps.rb:7
     Then $20 should be dispensed                            # features/step_defi nitions/cash_withdrawal_steps.rb:11
     And the balance of my account should be $80             # features/step_defi nitions/cash_withdrawal_steps.rb:15

 1 scenario (1 undefined) 4 steps (3 skipped, 1 undefined) 0m0.013s

 You can implement step definitions for undefined steps with these
 snippets:

 Given(/^I have deposited \$(\d+) in my Account$/) do |arg1|   pending
 \# express the regexp above with the code you wish you had end

我知道我必须在这里做错事,但我找不到什么,需要帮助。 Gemfile内容:

# This Gemfile lists all Gems used throughout the book - with versions.
source :rubygems

# Build stuff
gem 'bundler', '1.5.3'
#gem 'rake', '10.1.1'
#gem 'watchr', '0.7'
#gem 'bcat', '0.6.2'

# General stuff
#gem 'aruba', '0.4.11'
gem 'cucumber', '1.3.10', :require => 'cucumber'
gem 'rake', '10.1.1'
gem 'rspec', '2.14.1', :require => 'cucumber'
gem 'rspec-expectations', '2.14.5'
gem 'watir-webdriver', '0.6.7'

我想我已经包含了所需的所有信息。

3 个答案:

答案 0 :(得分:3)

我刚遇到同样的问题,我认为这是捆绑销商的问题。无论如何,我通过运行来解决了这个问题:

bundle update
bundle exec ruby <yourfilename.rb>

希望这有帮助!

答案 1 :(得分:0)

我之前遇到过同样的问题。我通过添加一个简单的

来修复它
require rubygems

在我所有需要的行代码之前。

答案 2 :(得分:0)

每当你安装一个新的gem时,你需要更新这个包(正如@zwolfe指出的那样)。 因此,运行:

bundle update

您应该能够使用简单的:

运行Ruby脚本

ruby <yourRubyScript.rb> 要么 bundle exec ruby <yourRubyScript.rb