0

私はMichaelHartlのチュートリアルを使用しています。テストを実行すると、次のエラーが発生します。

 1) Authentication authorization for non-signed-in users when attempting to visit a   
protected page as non-admin user submitting a DELETE request to the Users#destroy action
Failure/Error: specify { response.should redirect_to(root_path) } 
Expected response to be a redirect to <http://www.example.com/> but
was a redirect to <http://www.example.com/users>
# ./spec/requests/authentication_pages_spec.rb:81:in `block (7 levels)
in <top required)>'

これが私のauthentication_pages_spec.rbです。コードは、チュートリアルの例に示されているものとは少し異なる場合があります。これらの変更は、最近のテストに合格するために必要でした。この瞬間まで、すべてが完璧に機能しました。

require 'spec_helper'

describe "Authentication" do
  let(:user) { FactoryGirl.create(:user) } 
    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 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
        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) } 

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

  describe "authorization" do

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

      describe "when attempting to visit a protected page" do
        before do
          visit edit_user_path(user)
          fill_in "Email",    with: user.email
          fill_in "Password", with: user.password
          click_button "Sign in"
        end

        describe "after signing in" do

          it "should render the desired protected page" do
            page.should have_selector('title', text: 'Edit user')
          end          
       end
        describe "as non-admin user" do
          let(:user)  { FactoryGirl.create(:user) }
          let(:non_admin) { FactoryGirl.create(:user) }

          before { sign_in non_admin }

          describe "submitting a DELETE request to the Users#destroy action" do
            before { delete user_path(user) }
            specify { response.should redirect_to(root_path) }
          end
        end        
      end
    end

    describe "in the Users controller" do

      describe "visiting the edit page" do
        before { visit edit_user_path(user) }
        it { should have_selector('title', text: "Sign in") }
      end

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

      describe "visiting the user index" do
        before {visit users_path }
        it { should have_selector('title', text: 'Sign in') }
      end
    end


     describe "as wrong user" do
       let(:user) { FactoryGirl.create(:user) }
       let(:wrong_user) { FactoryGirl.create(:user, email: "wrong@example.com") }
       before { sign_in user }

       describe "visiting User#edit page" do
         before { visit edit_user_path(wrong_user) }
         it { should_not have_selector('title', text: full_title('Edit user')) }
       end

       describe "submitting a PUT request to the User#update action" do
         before { put user_path(wrong_user) }
         specify { response.should redirect_to(root_path) } 
       end
    end    
  end
end

誰かアイデアがありますか?ヘルプと関心をありがとう!

4

2 に答える 2

3

users_controller#destroyで、 (期待している)users_pathの代わりにリダイレクトします。root_path#destroyアクションを見せてもらえますか?

于 2012-10-08T12:09:24.147 に答える
0

私はこの章を読み終えたばかりで同じ問題を抱えていましたが、受け入れられた答えは正しいとは思いません。その理由は次のとおりです。

ユーザーコントローラーの破棄アクションは、root_pathではなくusers_pathにリダイレクトする必要があります。これは、ユーザーを削除した後に管理者をユーザーのリストに戻し、DELETEを発行しようとしている非管理者をリダイレクトするためです。ホームページに戻ってリクエストします。

セクションを読み直した後、ユーザーコントローラーのbeforeフィルターを見逃していることに気付きました。

before_action :admin_user, only: :destroy

それを追加すると、テストに合格しました。

于 2013-07-28T19:13:35.083 に答える