0

I'm in section 7.2.2 (Chapter 7) of Michael Hartl's Rails Tutorial, and when I add an @user variable to app/controllers/users_controller.rb things get strange. The tests that are supposed to now pass don't - they instead fail for a different reason:

$ 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)>'

Initially, these tests fail b/c @user is nil. Now they're failing due to an undefined method.

The book suggests that they should pass once I've defined @user, so I'm confused. I've pushed ahead a bit thinking perhaps this would work itself out, but it hasn't, so I'd like to figure this out before proceeding.

Here are the tests in question (copied from the book):

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 
4

1 回答 1

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
于 2012-06-20T03:00:30.043 回答