私はTurorial「Ruby on Railsに 『マイケル・ハートルのを読んでいる』が、私はこだわっているhere RSpecのは - テストが失敗した場合でも(ログインしているユーザーをシミュレートする)アプリケーションの作品
は、私がテストを書きました。- は、ユーザーがログインして、それがログインページ にリダイレクトのアクセス
- ユーザーがロギングしている場合dと、彼は別のユーザーが、問題はポイント2をテスト仕様であるルート
users#edit
または
users#update
にしようとされていない場合:ユーザーの編集/更新時にユーザー認証をチェックするために、私はまた、働くコントローラのコードと
を書いてきましたにリダイレクトされて編集しようと、私はuser_path
にPUTリクエストを送信し、私はルートにリダイレクトさが、実行していることを確認してくださいRSpecの私は次のエラーを取得:
Failures:
1) Authentication authorization as wrong user submitting a PUT request to users#update
Failure/Error: specify { response.should redirect_to(root_url) }
Expected response to be a redirect to <http://www.example.com/> but was a redirect to <http://www.example.com/signin>
# ./spec/requests/authentication_pages_spec.rb:73:in `block (5 levels) in <top (required)>'
をユーザーが全く記録されていない場合のように、それは、代わりにroot_url
のsignin_url
にリダイレクトされますようだ...これは私が問題原因を推測するものです。
アプリケーションは、ユーザーを認証するためにトークンをCookieに格納します。あなたは方法はput
を用いた場合でも、ユーザーがログインしていることを確認するremember_tokenクッキーを設定見ることができるように
# spec/support/utilities.rb
def sign_in(user)
visit signin_path
fill_in "Email", with: user.email
fill_in "Password", with: user.password
click_button "Sign in"
# Make signin work even when not using capybara (e.g. when using get, or post)
cookies[:remember_token] = user.remember_token
end
、delete
、この私はsignin_user(user)
と呼ばれるヘルパーメソッドを書き、spec/support/utilities.rb
ファイルにそれを置くをテストする 等...または少なくともそれは動作する必要があります!
どうすればこの仕様を通過させることができますか?
# spec/requests/authentication_pages_spec.rb
require 'spec_helper'
describe "Authentication" do
subject { page }
# ...
describe "authorization" do
# ...
describe "as wrong user" do
let(:user) { FactoryGirl.create(:user) }
let(:another_user) { FactoryGirl.create(:user, email: "[email protected]") }
before { sign_in(user) }
# ...
describe "submitting a PUT request to users#update" do
before { put user_path(another_user) }
specify { response.should redirect_to(root_url) }
end
end
end
end
UPDATE:これは私のUsersController
次のとおりです。
class UsersController < ApplicationController
before_filter :signed_in_user, :only => [:edit, :update]
before_filter :correct_user, :only => [:edit, :update]
def show
@user = User.find(params[:id])
end
def new
@user = User.new
end
def create
@user = User.new(params[:user])
if @user.save
sign_in @user
flash[:success] = "Welcome to the sample App!"
redirect_to @user
else
render "new"
end
end
def edit
end
def update
if @user.update_attributes(params[:user])
sign_in(@user)
flash[:success] = "Your profile was successfully updated"
redirect_to @user
else
render "edit"
end
end
private
def signed_in_user
unless signed_in?
redirect_to signin_url, notice: "Please sign in to access this page."
end
end
def correct_user
@user = User.find(params[:id])
redirect_to root_url unless current_user?(@user)
end
end
あなたはコントローラが動作すると言いましたが、とにかくユーザーコントローラのコードを投稿できますか? 'edit' *と' update'アクションの両方が、あなたの2番目のポイントの 'root_url'にリダイレクトされていますか? – pjumble
@pjumble返信いただきありがとうございます。質問をUsersControllerのコードで更新していることを確認してください。私はブラウザでテストしました。 –
@pjumbleあなたの質問には、wgetでPUTリクエストをする方法がわかりません。マニュアルページで検索しましたが、何も見つかりませんでした。 –