2013-07-02 84 views
0

對Rails來說很新穎,我無法真正弄清楚爲什麼在Hartl的Rails教程中我的測試達不到8.25之前我的測試無法通過。這裏是錯誤和一些代碼。我認爲這可能是一些微不足道的事情,但我已經多次瀏覽過,但我仍然無法弄清楚。任何幫助是極大的讚賞 !另外,請讓我知道我是否應該添加更多的代碼Ruby Rails教程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: "[email protected]" 
      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('[email protected]') } 

      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 

應用程序/傭工/ 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 
+3

您應該粘貼'/應用/助理/ sessions_helper。 rb'。 –

+0

另外,無論你的根路徑模板是什麼。 – claptimes

+0

您很可能試圖用符號(':a_symbol')而不是整數('42')將數組索引到數組中。 –

回答

2

SessionsHelper#current_user你有行:

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

由於cookies是返回餅乾HashHashWithIndifferentAccess,要準確)的方法,它應該是:

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

這樣做幾乎修復了我的錯誤,但現在我剩下兩個了。他們被編輯在 –

+0

@ErvNoel我建議創建一個新的問題,因爲你以前的問題現在已經解決了。 –