我正在跟随Michael Hartl的“Ruby on Rails Tutorial 3rd Edition”。我在第3章,第一次尝试使用测试命令。
有两个非常基本的页面 - 家庭和帮助 - 只有文字。我认为以下测试应该检查页面是否存在:
sample_app/test/controllers/static-pages.rb
require 'test_helper'
class StaticPagesControllerTest < ActionController::TestCase
test "should get home" do
get :home
assert_response :success
end
test "should get help" do
get :help
assert_response :success
end
end
但是,当我运行命令
时$ bundle exec test rake
在终端窗口中,我收到一个以
开头的长错误rake aborted!
NoMethodError: undefined method `web_console' for ActiveRecord::Base:Class
我已经阅读了各种似乎围绕着宝石文件的建议,尤其是“web-console”。我试过移动宝石有点无济于事但我愿意再试一次。这是当前的gem文件:
sample_app/Gemfile
(已删除评论)
source 'https://rubygems.org'
gem 'rails', '4.2.2'
gem 'sqlite3'
gem 'sass-rails', '~> 5.0'
gem 'uglifier', '>= 1.3.0'
gem 'coffee-rails', '~> 4.1.0'
gem 'jquery-rails'
gem 'turbolinks'
gem 'jbuilder', '~> 2.0'
gem 'sdoc', '~> 0.4.0', group: :doc
gem 'web-console', group: :development
group :development do
gem 'sqlite3'
gem 'byebug'
gem 'spring'
gem 'guard'
gem 'guard-minitest'
end
group :test do
gem 'minitest-reporters'
gem 'mini_backtrace'
end
如果您感兴趣,以下是完整的错误代码:
Running via Spring preloader in process 6353
rake aborted!
NoMethodError: undefined method `web_console' for ActiveRecord::Base:Class
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-4.2.2/lib/active_record/dynamic_matchers.rb:26:in `method_missing'
/home/ubuntu/workspace/sample_app/test/controllers/static_pages_controller_test.rb:16:in `<class:StaticPagesControllerTest>'
/home/ubuntu/workspace/sample_app/test/controllers/static_pages_controller_test.rb:5:in `<top (required)>'
/usr/local/rvm/gems/ruby-2.3.0/gems/railties-4.2.2/lib/rails/test_unit/sub_test_task.rb:114:in `require'
/usr/local/rvm/gems/ruby-2.3.0/gems/railties-4.2.2/lib/rails/test_unit/sub_test_task.rb:114:in `block (3 levels) in define'
/usr/local/rvm/gems/ruby-2.3.0/gems/railties-4.2.2/lib/rails/test_unit/sub_test_task.rb:114:in `each'
/usr/local/rvm/gems/ruby-2.3.0/gems/railties-4.2.2/lib/rails/test_unit/sub_test_task.rb:114:in `block (2 levels) in define'
/usr/local/rvm/gems/ruby-2.3.0/gems/railties-4.2.2/lib/rails/test_unit/sub_test_task.rb:113:in `each'
/usr/local/rvm/gems/ruby-2.3.0/gems/railties-4.2.2/lib/rails/test_unit/sub_test_task.rb:113:in `block in define'
/usr/local/rvm/gems/ruby-2.3.0/gems/railties-4.2.2/lib/rails/test_unit/sub_test_task.rb:20:in `invoke_rake_task'
/usr/local/rvm/gems/ruby-2.3.0/gems/railties-4.2.2/lib/rails/test_unit/testing.rake:8:in `block in <top (required)>'
/usr/local/rvm/gems/ruby-2.3.0/gems/rake-12.0.0/exe/rake:27:in `<top (required)>'
/usr/local/rvm/gems/ruby-2.3.0/bin/ruby_executable_hooks:15:in `eval'
/usr/local/rvm/gems/ruby-2.3.0/bin/ruby_executable_hooks:15:in `<main>'
Tasks: TOP => test:run
(See full trace by running task with --trace)
答案 0 :(得分:0)
我可能找到了答案:注销Cloud9。关掉电脑。重启电脑。重新登录Cloud9。输入“bundle exec rake test”。而且它是固定的。魔术!
更严重的是,我意识到我正在使用旧版本的教程,但教程确实指导学生以适合此版本的方式设置环境(我相信)。尽管如此,我确实看到Cloud9在某些时候发出警告,我使用的Ruby(2.3.0)版本是“buggy”并将其升级到2.3.1。我不知道Ruby 2.3.0的错误是否是Cloud9中的罪魁祸首或者是错误的,但对于使用旧版本教程的其他人来说,这是值得注意的。