【问题标题】:Getting errors when using rspec使用 rspec 时出现错误
【发布时间】:2015-04-15 22:00:53
【问题描述】:

我正在编写有关 rspec 的教程,并且每当我尝试执行 rake 测试或通过终端手动运行 rspec 时,都会不断出错。这是我得到的:

mes-mbp:00_hello Me$ ls
hello.rb    hello_spec.rb   index.html
mes-mbp:00_hello Me$ rspec 00_hello/hello_spec.rb
/Users/Me/.rvm/rubies/ruby-2.2.0/lib/ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require': cannot load such file -- spec_helper (LoadError)
    from /Users/Me/.rvm/rubies/ruby-2.2.0/lib/ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require'
    from /Users/Me/.rvm/gems/ruby-2.2.0/gems/rspec-core-3.2.3/lib/rspec/core/configuration.rb:1181:in `block in requires='
    from /Users/Me/.rvm/gems/ruby-2.2.0/gems/rspec-core-3.2.3/lib/rspec/core/configuration.rb:1181:in `each'
    from /Users/Me/.rvm/gems/ruby-2.2.0/gems/rspec-core-3.2.3/lib/rspec/core/configuration.rb:1181:in `requires='
    from /Users/Me/.rvm/gems/ruby-2.2.0/gems/rspec-core-3.2.3/lib/rspec/core/configuration_options.rb:110:in `block in process_options_into'
    from /Users/Me/.rvm/gems/ruby-2.2.0/gems/rspec-core-3.2.3/lib/rspec/core/configuration_options.rb:109:in `each'
    from /Users/Me/.rvm/gems/ruby-2.2.0/gems/rspec-core-3.2.3/lib/rspec/core/configuration_options.rb:109:in `process_options_into'
    from /Users/Me/.rvm/gems/ruby-2.2.0/gems/rspec-core-3.2.3/lib/rspec/core/configuration_options.rb:22:in `configure'
    from /Users/Me/.rvm/gems/ruby-2.2.0/gems/rspec-core-3.2.3/lib/rspec/core/runner.rb:96:in `setup'
    from /Users/Me/.rvm/gems/ruby-2.2.0/gems/rspec-core-3.2.3/lib/rspec/core/runner.rb:85:in `run'
    from /Users/Me/.rvm/gems/ruby-2.2.0/gems/rspec-core-3.2.3/lib/rspec/core/runner.rb:70:in `run'
    from /Users/Me/.rvm/gems/ruby-2.2.0/gems/rspec-core-3.2.3/lib/rspec/core/runner.rb:38:in `invoke'
    from /Users/Me/.rvm/gems/ruby-2.2.0/gems/rspec-core-3.2.3/exe/rspec:4:in `<top (required)>'
    from /Users/Me/.rvm/gems/ruby-2.2.0/bin/rspec:23:in `load'
    from /Users/Me/.rvm/gems/ruby-2.2.0/bin/rspec:23:in `<main>'
    from /Users/Me/.rvm/gems/ruby-2.2.0/bin/ruby_executable_hooks:15:in `eval'
    from /Users/Me/.rvm/gems/ruby-2.2.0/bin/ruby_executable_hooks:15:in `<main>'

谁能告诉我我做错了什么?

编辑:有关我按要求执行的操作的更多信息: 我正在关注this 教程。在上面的代码中,我试图对 hello_spec.rb 文件中的 hello.rb 文件运行测试,该文件中包含“require hello”代码。

【问题讨论】:

  • 你能发布更多关于你在做什么的信息吗?
  • touch spec_helper.rb
  • 我根本看不到spec_helper 文件。这甚至看起来不像是一个 Rails 应用程序。也许是辛纳特拉?您还需要在项目的根目录下运行 rspec --init。这将创建像spec_helper 这样的文件。 See Here 了解更多信息。
  • 那些教程已经过时了——我会推荐类似codeschool.com/courses/testing-with-rspec的东西。您绝对不想开始使用 RSpec 2,然后必须学习新的 RSpec 3 语法。

标签: ruby-on-rails ruby rspec


【解决方案1】:

您给 RSpec 提供了错误的规范文件路径。

当前工作目录是00_hello

mes-mbp:00_hello Me$ rspec 00_hello/hello_spec.rb

将导致 RSpec 查找 00_hello\00_hello\hello_spec.rb

如果您运行了rspec --init,则可能有一个.rspec 文件,如果它包含以下行,则需要spec_helper.rb

--require spec_helper

编写规范时最常见的做法是将规范放在名为spec 的目录中。这样你就可以通过

运行所有的项目规范
 $ rspec specs

补充:

这是 gems 和其他 ruby​​ 项目非常常见的结构:

lib/
   hello.rb
spec/
   spec_helper.rb
   hello_spec.rb

RSpec 会自动将/lib/spec 目录添加到加载路径中。

所以你可以这样做:

# spec/hello_spec.rb
require 'spec_helper'
require 'lib/hello' # instead of a relative path! Wehoo.

RSpec.describe Hello do
  # ...
end

【讨论】:

    猜你喜欢
    • 2015-09-26
    • 1970-01-01
    • 2017-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多