0

Rails 很新,我真的不明白为什么我的测试在 Hartl's Rails Tutorial 中的 8.25 之前不能通过。这是错误和一些代码。我认为这可能是我可能忽略的一些微不足道的事情,但我已经看过好几次了,但我仍然无法弄清楚。任何帮助是极大的赞赏 !另外,如果我应该添加更多代码,请告诉我

1) Authentication signin with valid information 
 Failure/Error: it { should have_link('Settings', href: edit_user_path(user)) }
   expected link "Settings" to return something
 # ./spec/requests/authentication_pages_spec.rb:41:in `block (4 levels) in <top (required)>'

  2) Authentication signin with valid information 
 Failure/Error: it { should have_link('Users',    href: users_path) }
   expected link "Users" to return something
 # ./spec/requests/authentication_pages_spec.rb:39:in `block (4 levels) in <top (required)>'

Finished in 2.38 seconds
56 examples, 2 failures

Failed examples:

rspec ./spec/requests/authentication_pages_spec.rb:41 # Authentication signin with valid information 
rspec ./spec/requests/authentication_pages_spec.rb:39 # Authentication signin with valid information 

user_pages_spec.rb

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_selector('h1', text: user.name) }
    it { should have_selector('title', text: user.name) }
  end

  describe "signup page" do
    before { visit signup_path }

    it { should have_selector('h1',    text: 'Sign up') }
    it { should have_selector('title', text: full_title('Sign up')) }
  end


    describe "signup" 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

        describe "after submission" do
          before { click_button submit }

          it { should have_selector('title', text: 'Sign up') }
          it { should have_content('error') }
        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

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

        describe "after saving the user" do
          before { click_button submit }

          let(:user) { User.find_by_email('user@example.com') }

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

authentication_pages_spec.rb

require 'spec_helper'

describe "Authentication" do

  subject { page }

  describe "signin page" do
    before { visit signin_path }

    it { should have_selector('h1',    text: 'Sign in') }
    it { should have_selector('title', text: 'Sign in') }
  end

  describe "signin" do
    before { visit signin_path }

    describe "with invalid information" do
      before { click_button "Sign in" }

      it { should have_selector('title', text: 'Sign in') }
      it { should have_selector('div.alert.alert-error', text: 'Invalid') }

      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

      it { should have_selector('title', text: user.name) }

      it { should have_link('Users',    href: users_path) }
      it { should have_link('Profile',  href: user_path(user)) }
      it { should have_link('Settings', href: edit_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

应用程序/helpers/session_helper.rb

module SessionsHelper

  def sign_in(user)
    cookies.permanent[:remember_token] = user.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
    @current_user ||= User.find_by_remember_token(:cookies[:remember_token])
  end
end
4

1 回答 1

2

SessionsHelper#current_user你有行:

@current_user ||= User.find_by_remember_token(:cookies[:remember_token])

既然cookies是返回 cookie 的方法HashHashWithIndifferentAccess准确地说是 ),它应该是:

@current_user ||= User.find_by_remember_token(cookies[:remember_token])
于 2013-07-04T12:48:32.027 回答