Rails教程7.2.2 UsersController问题

时间:2012-06-19 22:59:32

标签: rspec ruby-on-rails-3.2 railstutorial.org

我在迈克尔·哈特尔的Rails教程的第7.2.2节(第7章)中,当我向app/controllers/users_controller.rb添加@user变量时,事情变得奇怪。原本应该通过的测试不会 - 它们会因为不同的原因而失败:

$ rspec spec/requests/user_pages_spec.rb -e "signup page"

Failures:

1) User pages signup page 
 Failure/Error: before { visit signup_path }
 ActionView::Template::Error:
   undefined method `users_path' for #<#<Class:0x007fdec372a8e0>:0x007fdec375d010>
 # ./app/views/users/new.html.erb:6:in `_app_views_users_new_html_erb___2884421820517152565_70297369620580'
 # ./spec/requests/user_pages_spec.rb:8:in `block (3 levels) in <top (required)>'

2) User pages signup page 
 Failure/Error: before { visit signup_path }
 ActionView::Template::Error:
   undefined method `users_path' for #<#<Class:0x007fdec372a8e0>:0x007fdec45caee0>
 # ./app/views/users/new.html.erb:6:in `_app_views_users_new_html_erb___2884421820517152565_70297369620580'
 # ./spec/requests/user_pages_spec.rb:8:in `block (3 levels) in <top (required)>'

最初,这些测试失败b / c @user是nil。现在他们由于未定义的方法而失败。

这本书表明,一旦我定义了@user,它们应该通过,所以我很困惑。我已经向前推了一下,想也许这会自行解决,但事实并非如此,所以我想在继续之前弄清楚这一点。

以下是有问题的测试(从书中复制):

require 'spec_helper'

describe "User pages" do
  subject { page }

  describe "signup page" do
   let(:user) { FactoryGirl.create(:user) }
   before { visit signup_path }
   it { should have_selector('h1', text: 'Sign up') }
   it { should have_selector('title', text: full_title('Sign up')) }
  end
end 

1 个答案:

答案 0 :(得分:1)

从头开始重新开放项目我遇到了一个不同的错误,导致我在config/environments/test.rb发现了这个错字:

# Speed up tests by lowering BCrypt's cost function. require 'bcrypt'
silence_warnings do
  BCrypt::Engine::DEFAULT_COST = BCrypt::Engine::MIN_COST
end

通过将require 'bcrypt'移动到它自己的行,一切都会再次起作用 - 包括原始问题中的测试。

# Speed up tests by lowering BCrypt's cost function. 
require 'bcrypt'
silence_warnings do
  BCrypt::Engine::DEFAULT_COST = BCrypt::Engine::MIN_COST
end