Rails Tutorial chapter 9.1.1 sign_in unrecognised

给你一囗甜甜゛ 提交于 2019-12-11 04:59:01

问题


I'm working through Michael Hartl's tutorial.

I've just introduced these tests to user_pages_spec.rb

describe "edit" do
  let(:user) { FactoryGirl.create(:user) }
  before do
    sign_in user
    visit edit_user_path(user)
  end

  describe "page" do
    it { should have_content("Update your profile") }
    it { should have_title("Edit user") }
    it { should have_link('change', href: 'http://gravatar.com/emails') }
  end

  describe "with invalid information" do
    before { click_button "Save changes" }

    it { should have_content('error') }
  end
end

My sessions_helper.rb file is:

module SessionsHelper
  def sign_in(user)
    remember_token = User.new_remember_token
    cookies.permanent[:remember_token] = remember_token
    user.update_attribute(:remember_token, User.encrypt(remember_token))
    self.current_user= user
  end

  def current_user=(user)
    @current_user=user
  end

  def current_user
    remember_token = User.encrypt(cookies[:remember_token])
    @current_user ||= User.find_by(remember_token: remember_token)
  end

  def signed_in?
    !current_user.nil?
  end

  def sign_out
    self.current_user = nil
    cookies.delete(:remember_token)
  end
end

When I run the tests, all 4 fail with error messages along the lines of:

1) User pages edit page 
   Failure/Error: sign_in user
   NoMethodError:
     undefined method `sign_in' for #<RSpec::Core::ExampleGroup::Nested_1::Nested_4::Nested_1:0x007fcd57928138>
   # ./spec/requests/user_pages_spec.rb:90:in `block (3 levels) in <top (required)>'

How should I get the user pages specs to pick up the sign_in function from sessions_helper? users_controller can use it; it's just the test files that I've done wrong.

I tried including SessionsHelper explicitly and it didn't help.


回答1:


The introduction of the sign_in user call in Listing 9.1 was a recent change that was inconsistent with the fact that this method isn't introduced for use by RSpec until Listing 9.6. It turns out the original version was correct and that call to sign_in user has since been removed.



来源:https://stackoverflow.com/questions/18860783/rails-tutorial-chapter-9-1-1-sign-in-unrecognised

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!