我正在关注RSpec测试的一章,但我不断得到load error
。
它首先告诉我创建一个新分支,然后创建两个新目录:Models
和Spec
。在这些目录中,我应该制作两个文件; models/entry.rb
和spec/entry_spec.rb
。在spec/entry_spec.rb
中,我应该输入以下代码:
require_relative '../models/entry'
RSpec.describe Entry do
end
然后在我的models/entry.rb
我应该把这段代码:
class Entry
end
然后我应该运行这个终端命令:
$ rspec spec/entry_spec.rb
得到这个结果:
No examples found.
Finished in 0.00015 seconds (files took 0.1186 seconds to load)
0 examples, 0 failures
但我总是得到这个加载错误,我不知道这是什么意思
/Users/ethanfranson/.rvm/rubies/ruby-2.2.1/lib/ruby/site_ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require': cannot load such file -- spec_helper (LoadError)
from /Users/ethanfranson/.rvm/rubies/ruby-2.2.1/lib/ruby/site_ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require'
from /Users/ethanfranson/.rvm/gems/ruby-2.2.1/gems/rspec-core-3.4.2/lib/rspec/core/configuration.rb:1295:in `block in requires='
from /Users/ethanfranson/.rvm/gems/ruby-2.2.1/gems/rspec-core-3.4.2/lib/rspec/core/configuration.rb:1295:in `each'
from /Users/ethanfranson/.rvm/gems/ruby-2.2.1/gems/rspec-core-3.4.2/lib/rspec/core/configuration.rb:1295:in `requires='
from /Users/ethanfranson/.rvm/gems/ruby-2.2.1/gems/rspec-core-3.4.2/lib/rspec/core/configuration_options.rb:109:in `block in process_options_into'
from /Users/ethanfranson/.rvm/gems/ruby-2.2.1/gems/rspec-core-3.4.2/lib/rspec/core/configuration_options.rb:108:in `each'
from /Users/ethanfranson/.rvm/gems/ruby-2.2.1/gems/rspec-core-3.4.2/lib/rspec/core/configuration_options.rb:108:in `process_options_into'
from /Users/ethanfranson/.rvm/gems/ruby-2.2.1/gems/rspec-core-3.4.2/lib/rspec/core/configuration_options.rb:21:in `configure'
from /Users/ethanfranson/.rvm/gems/ruby-2.2.1/gems/rspec-core-3.4.2/lib/rspec/core/runner.rb:105:in `setup'
from /Users/ethanfranson/.rvm/gems/ruby-2.2.1/gems/rspec-core-3.4.2/lib/rspec/core/runner.rb:92:in `run'
from /Users/ethanfranson/.rvm/gems/ruby-2.2.1/gems/rspec-core-3.4.2/lib/rspec/core/runner.rb:78:in `run'
from /Users/ethanfranson/.rvm/gems/ruby-2.2.1/gems/rspec-core-3.4.2/lib/rspec/core/runner.rb:45:in `invoke'
from /Users/ethanfranson/.rvm/gems/ruby-2.2.1/gems/rspec-core-3.4.2/exe/rspec:4:in `<top (required)>'
from /Users/ethanfranson/.rvm/gems/ruby-2.2.1/bin/rspec:23:in `load'
from /Users/ethanfranson/.rvm/gems/ruby-2.2.1/bin/rspec:23:in `<main>'
from /Users/ethanfranson/.rvm/gems/ruby-2.2.1/bin/ruby_executable_hooks:15:in `eval'
from /Users/ethanfranson/.rvm/gems/ruby-2.2.1/bin/ruby_executable_hooks:15:in `<main>'
如果有人花时间帮我解决我可能做错的事情,我会非常感激。对不起,这个冗长的问题!
答案 0 :(得分:2)
仔细检查您是否从app目录的根目录运行rspec spec/entry_spec.rb
,而不是从spec
目录中运行。
Rspec自动将spec
添加到加载路径。如果您已经在spec
目录中,则rspec
会将spec/spec
添加到加载路径中。
答案 1 :(得分:2)
首先确保您已正确安装了rspec-rails:
rails g rspec:install
您不需要在/app
中要求您提供模型文件或任何其他文件。当您需要rails_helper
时,它们会自动加载。
# spec/models/entry_spec.rb
require 'rails_helper'
RSpec.describe Entry do
end
RSpec可以为您生成此规范:
rails generate rspec:model entry
您还应该在config/application.rb
:
# ...
config.generators do |g|
g.test_framework :rspec
end
这样当您运行常规轨道生成器RSpec(例如rails g model entry
)时,它也会为您创建规范文件。
请注意,旧版本的rspec-rails会require 'spec_helper'
,而在当前版本中,您会使用require 'rails_helper'
,因为它会将常规RSpec配置和rails设置分成两个单独的文件。