簡體   English   中英

Ruby Rails教程8.25

[英]Ruby Rails Tutorial 8.25

對Rails來說很新,我無法弄清楚為什么我的測試在Hartl的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: "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

應用程序/佣工/ 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

SessionsHelper#current_user你有一行:

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

由於cookies是返回cookie Hash的方法( HashWithIndifferentAccessHashWithIndifferentAccess ),它應該是:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM