Michael Hartl第8章无法退出

时间:2014-03-02 20:29:03

标签: ruby-on-rails rspec

点击退出后,当我在下拉框中登录我的应用程序时,它会将我带到错误页面。而不是将我重定向到我的根页面。我就在本章的最后。

以下是我得到的Rspec错误。

Failures:

  1) Authentication signin page with valid information followed by signout
     ←[31mFailure/Error:←[0m ←[31mbefore { click_link "Sign out" }←[0m
     ←[31mNoMethodError←[0m:
       ←[31mundefined method `update_attribute' for nil:NilClass←[0m
←[36m     # ./app/helpers/sessions_helper.rb:29:in `sign_out'←[0m
←[36m     # ./app/controllers/sessions_controller.rb:18:in `destroy'←[0m
←[36m     # ./spec/requests/authentication_pages_spec.rb:36:in `block (5 levels)
 in <top (required)>'←[0m

sessions_controllers

    class SessionsController < ApplicationController

  def new
  end

  def create
    user = User.find_by(email: params[:session][:email].downcase)
    if user && user.authenticate(params[:session][:password])
      sign_in user
      redirect_to user
    else
      flash.now[:error] = 'Invalid email/password combination'
      render 'new'
    end
  end

  def destroy
    sign_out
    redirect_to root_url
  end
end

users_controllers

class UsersController < ApplicationController

  def show 
    @user = User.find(params[:id])
  end

  def new
    @user = User.new
  end

  def create
    @user = User.new(user_params)
    if @user.save
      sign_in @user
      flash[:success] = "Welcome to the Sample App!"
      redirect_to @user
    else
      render 'new'
    end
  end

  private

    def user_params
        params.require(:user).permit(:name, :email, :password, :password_confirmation)
    end
end

sessions_helper

module SessionsHelper

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

      def signed_in?
        current_user.nil?
      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 sign_out
        self.current_user = nil
        cookies.delete(:remember_token)
      end

      def sign_out
        current_user.update_attribute(:remember_token,
                                      User.hash(User.new_remember_token))
        cookies.delete(:remember_token)
        self.current_user = nil
      end
    end

user_pages_spec

require 'spec_helper'

describe "User pages" do

  subject { page }

  describe "profile page" do
    let(:user) { FactoryGirl.create(:user) }
    before { visit user_path(user) }

    it { should have_content(user.name) }
    it { should have_title(user.name) }
  end

  describe "signup page" do
    before { visit signup_path }

    let(:submit) { "Create my account" }

    describe "with invalid information" do
      it "should not create a user" do
        expect { click_button submit }.not_to change(User, :count)
      end
    end

    describe "with valid information" do
      before do
        fill_in "Name",         with: "Example User"
        fill_in "Email",        with: "user@example.com"
        fill_in "Password",     with: "foobar"
        fill_in "Confirmation", with: "foobar"
      end

      describe "after saving the user" do
        before { click_button submit }
        let(:user) { User.find_by(email: 'user@example.com') }

        it { should have_link('Sign out') }
        it { should have_title(user.name) }
        it { should have_selector('div.alert.alert-success', text: 'Welcome') }
      end

      it "should create a user" do
        expect { click_button submit }.to change(User, :count).by(1)
      end
    end
  end
end

应用程序/模型/用户

class User < ActiveRecord::Base
  before_save { self.email = email.downcase }
  before_save :create_remember_token

  validates :name, presence: true, length: { maximum: 50 }
  VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
  validates :email, presence:   true,
                    format:     { with: VALID_EMAIL_REGEX },
                    uniqueness: { case_sensitive: false }
  has_secure_password
  validates :password, length: { minimum: 6 }

def User.new_remember_token
    SecureRandom.urlsafe_base64
  end

  def User.hash(token)
    Digest::SHA1.hexdigest(token.to_s)
  end

  private

    def create_remember_token
      self.remember_token = User.hash(User.new_remember_token)
    end
end

authentication_pages_spec

require 'spec_helper'

describe "Authentication" do

  subject { page }



    before { visit signin_path }

    describe "signin page" do
      before {visit signin_path}
      it {should have_content("Sign in")}
      it {should have_title("Sign in")}

    describe "with invalid information" do
      before { click_button "Sign in" }
      it { should have_title('Sign in') }
      it { should have_selector('div.alert.alert-error') }

      describe "after visiting another page" do
        before { click_link "Home" }
        it { should_not have_selector('div.alert.alert-error') }
      end
    end

    describe "with valid information" do
      let(:user) { FactoryGirl.create(:user) }
      before do
        fill_in "Email",    with: user.email.upcase
        fill_in "Password", with: user.password
        click_button "Sign in"
      end

      describe "followed by signout" do
        before { click_link "Sign out" }
        it { should have_link('Sign in') }
      end

      it { should have_title(user.name) }
      it { should have_link('Profile',     href: user_path(user)) }
      it { should have_link('Sign out',    href: signout_path) }
      it { should_not have_link('Sign in', href: signin_path) }
    end
  end
end

1 个答案:

答案 0 :(得分:0)

您已在会话助手中定义了两个sign_out方法。

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

  def sign_out
    current_user.update_attribute(:remember_token,
                                  User.hash(User.new_remember_token))
    cookies.delete(:remember_token)
    self.current_user = nil
  end

第二个是给你错误的,因为它试图update_attribute你在第一个方法中删除的当前用户。我想如果你只是删除它并使用更简单的第一个版本,测试应该通过。