ruby-on-rails - ruby on rails rspec 错误

标签 ruby-on-rails ruby ruby-on-rails-3 rspec

我正在阅读“通过示例学习 Rails”一书,并尝试运行测试。出于某种原因,我无法让 rspec 正常工作。

如果我按照他的指示运行 rspec spec/ 命令,我会收到以下错误:

$ rspec spec/
/home/desktop/.rvm/gems/ruby-1.9.2-p136/gems/bundler-1.0.21/lib/bundler/runtime.rb:31:in `block in setup': 
You have already activated rspec-core 2.7.1, but your Gemfile requires rspec-core 2.6.4. 
Using bundle exec may solve this. (Gem::LoadError)

奇怪的是我的 Gemfile 没有指定版本--

group :development do
  gem 'rspec-rails'
end

group :test do
  gem 'rspec'
  gem 'webrat'
end

如果我遵循错误消息中的建议并使用 bundle exec rspec spec/ 则前两个测试通过——但是我们在教程中构建的新“关于”页面失败并显示以下内容错误,尽管据我所知我构建的页面(和 Controller 操作等)完全符合它们应该的样子:

Failures:

  1) PagesController GET 'about' should be successful
     Failure/Error: response.should_be_success
     NoMethodError:
       undefined method `should_be_success' for #<ActionController::TestResponse:0x00000003539438>
     # ./spec/controllers/pages_controller_spec.rb:23:in `block (3 levels) in <top (required)>'

Finished in 0.10861 seconds
3 examples, 1 failure

Failed examples:

rspec ./spec/controllers/pages_controller_spec.rb:21 # PagesController GET 'about' should be successful

我是一个非常有经验的程序员,但是我遇到了无穷无尽的问题,比如冲突的 gem 版本和一百种不同的方法来使用 Rails 完成所有不同的任务(例如“使用 RVM”,“不要使用 RVM” 、“使用 sudo 安装 gems”、“不要使用 sudo 安装 gems”等)

我的开发机器正在运行 ubuntu linux。

感谢您的帮助——请用 Ruby noob 语言解释我做错了什么!

最佳答案

运行 bundle exec 是正确的,并且是必需的,因为您安装了该 gem 的更新版本,而不是 Gemfile.lock 中指定的版本。使用 bundle exec 覆盖加载路径,导致仅加载 Gemfile.lock 中指定的 gem。 (您可能会发现将 bundle exec 别名为更短的名称很方便。)

第二个问题的答案就在错误信息中:

undefined method `should_be_success'

应该是should be_success

关于ruby-on-rails - ruby on rails rspec 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7860778/

相关文章:

html - Rails CSS、JS 和 jQuery TypeErrors(适用于 Windows)

ruby-on-rails - 如何在生成的 HTML 本身中显示部分的名称

ruby - ruby 中的别名 def []=(key, value) 方法

ruby-on-rails - Rails 3 -- Bundler/Capistrano 错误

mysql - 在带有 order by 的 Left join 中,始终从关系表中获取第一个数据。 ( rails )

ruby-on-rails - RSpec 测试不调用 Controller

javascript - 延迟加载模型中的关联

ruby-on-rails - 与 MRI Ruby 的并发请求

ruby - 为什么#each_with_object 和#inject 会切换 block 参数的顺序?

javascript - 如何在我的 create.js.erb 中重置/清除表单?