GabrielG
GabrielG

Reputation: 189

Rails Tutorial chapter 9.1.1 sign_in unrecognised

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.

Upvotes: 1

Views: 259

Answers (1)

Peter Alfvin
Peter Alfvin

Reputation: 29439

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.

Upvotes: 1

Related Questions