Ruby on Rails(Michael Hart教程)授权错误(第9.2.1章)

时间:2013-06-21 09:31:27

标签: ruby-on-rails authentication authorization

我正在从Michael Hartl的教程中学习Ruby on Rails 现在我坚持第9.2.1章 这是本书中的代码:

authentication_pages_spec.erb     需要'spec_helper'

describe "Authentication" do

  subject { page }

  describe "signin" do
    before { visit signin_path }

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

      it { should have_title('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
  end

  describe "with valid information" do
    let(:user) { FactoryGirl.create(:user) }
    before { sign_in user }

    it { should have_title(user.name) }
    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) }

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

    describe "for non-signed-in users" do
      let(:user) { FactoryGirl.create(:user) }

      describe "in the Users controller" do

        describe "visiting the edit page" do
          before { visit edit_user_path(user) }
          it { should have_title('Sign in') }
        end

        describe "submitting to the update action" do
          before { put user_path(user) }
          specify { response.should redirect_to(signin_path) }
        end
      end
    end
  end
end

但是当我测试时:

rspec spec/

我有2个错误,我不知道如何修复:

it { should have_title('Sign in') }

和行:

specify { response.should redirect_to(signin_path) }

这是错误图片: http://i.stack.imgur.com/85WaX.png

1 个答案:

答案 0 :(得分:0)

您在before { sign_in user }阻止内错过了describe "visiting the edit page" do

但是您已经正确地将其用于其他块,例如:

let(:user) { FactoryGirl.create(:user) }
before { sign_in user }