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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21831880/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 06:23:48  来源:igfitidea点击:

kernel_require.rb:55:in `require': cannot load such file error

rubycucumber

提问by Kita

I'm using Ruby version 1.9.3 at the moment (although I get the same issue with Ruby 2.0.0). On Windows 7 64-bit. I'm following 'The Cucumber Book' and got stuck at the chapter 7.2 - "Removing Duplication with Transforms". My folder structure is as follows:

我目前使用的是 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

In my cash_withdrawal_steps.rbfile I have:

在我的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

When I run cucumberI get:

当我运行时,cucumber我得到:

C:\Users\Nikita.Harrison\AutomatedTesting\cash_withdrawal>cucumber cannot load such file -- CAPTURE_CASH_AMOUNT (LoadError) C:/Ruby193/lib/ruby/site_ruby/1.9.1/rubygems/core_ext/kernel_require.rb:55:in r equire' C:/Ruby193/lib/ruby/site_ruby/1.9.1/rubygems/core_ext/kernel_require.rb:55:in r equire' 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:inload' 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:inload_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:ineach' 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: inload_step_definitions' C:/Ruby193/lib/ruby/gems/1.9.1/gems/cucumber-1.3.10/lib/cucumber/runtime.rb:42:i n run!' C:/Ruby193/lib/ruby/gems/1.9.1/gems/cucumber-1.3.10/lib/cucumber/cli/main.rb:47: inexecute!' C:/Ruby193/lib/ruby/gems/1.9.1/gems/cucumber-1.3.10/bin/cucumber:13:in <top (re quired)>' C:/Ruby193/bin/cucumber:23:inload' C:/Ruby193/bin/cucumber:23:in `'

C:\Users\Nikita.Harrison\AutomatedTesting\cash_withdrawal>cucumber 无法加载此类文件 -- CAPTURE_CASH_AMOUNT (LoadError) C:/Ruby193/lib/ruby/site_ruby/1.9.1/rubygems/core_ext/kernel_require.rb:55:in 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_definitions/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:inload' C:/Ruby193/lib/ruby/gems/1.9.1/gems/cucumber-1.3 .10/lib/cucumber/rb_support/rb_language.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:inload_file' C:/Ruby193/lib/ruby/gems/1.9.1/gems/cucumber-1.3.10/lib/cucumber/runtime/support _code .rb:83:在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: inload_step_definitions' C:/ ruby193/lib/ruby/gems/1.9.1/gems/cucumber-1.3.10/lib/cucumber/runtime.rb:42:inrun!' 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:in <top (re quired)>' C:/Ruby193/bin/cucumber:23:inload' C:/Ruby193/bin/cucumber:23:in `'

And if I run irbthen run require "CAPTURE_CASH_AMOUNT"I get this error:

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

irb(main):006:0> require "CAPTURE_CASH_AMOUNT" LoadError: cannot load such file -- CAPTURE_CASH_AMOUNT from 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:inrequire' from (irb):6 from C:/Ruby193/bin/irb:12:in `'

irb(main):006:0> require "CAPTURE_CASH_AMOUNT" LoadError: cannot load such file -- CAPTURE_CASH_AMOUNT from C:/Ruby193/lib/ruby/site_ruby/1.9.1/rubygems/core_ext/kernel_requir e.rb:55:inrequire' 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:in `'

I've tried many fixes including 'require_relative' and nothing seems to solve my problem. If I remove require 'CAPTURE_CASH_AMOUNT'from the cash_withdrawal_steps.rbfile and run cucumber then my 'Step Definition' doesn't show as defined:

我尝试了许多修复程序,包括“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 0 in my Account               # features\cash_with drawal.feature:5
     When I withdraw                                      # features/step_defi nitions/cash_withdrawal_steps.rb:7
     Then  should be dispensed                            # features/step_defi nitions/cash_withdrawal_steps.rb:11
     And the balance of my account should be              # 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

If I add require File.join(File.dirname("C:/Users/Nikita.Harrison/AutomatedTesting/cash_withdrawal/features/support"), 'support', 'transforms')to the env.rbfile and run cucumberI get:

如果我添加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 0 in my Account               # features\cash_with drawal.feature:5
     When I withdraw                                      # features/step_defi nitions/cash_withdrawal_steps.rb:7
     Then  should be dispensed                            # features/step_defi nitions/cash_withdrawal_steps.rb:11
     And the balance of my account should be              # 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

I know I must be doing something wrong here but I just can't find out what and need help. Gemfile contents:

我知道我一定在这里做错了什么,但我就是找不到什么需要帮助。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'

I think I've included all the information that is needed.

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

回答by zwolfe

I just encountered the same issue, and I believe it's a problem with bundler. In any case, I solved the problem by running:

我刚刚遇到了同样的问题,我相信这是捆绑程序的问题。无论如何,我通过运行解决了这个问题:

bundle update
bundle exec ruby <yourfilename.rb>

Hope this helps!

希望这可以帮助!

回答by SimionZ

Whenever you install a new gem you need to update the bundle (as @zwolfe pointed out). Hence, run:

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

bundle update

bundle update

Right after you should be able to run your Ruby scripts either using simply:

紧接着您应该能够使用简单的方式运行您的 Ruby 脚本:

ruby <yourRubyScript.rb>or bundle exec ruby <yourRubyScript.rb

ruby <yourRubyScript.rb>或者 bundle exec ruby <yourRubyScript.rb

回答by Fabrice31

It seems I encountered the same issue before. I fixed it by adding a simple

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

require rubygems

before all my require line code.

在我所有的 require 行代码之前。