1

コントローラーテスト関連のパスワード更新テストを書きたいです。コントローラーの最初の行で、where 条件を使用して認証済みの人物を見つけます。この行に関連するテストを書くにはどうすればよいですか。何も思いつきませんでした。

ChangePasswordsController

def update
  person = Person.where(_id: session[:user_id]).first
  identity = Identity.where(_id: person.user_id).first
  unless params[:new_password] != params[:new_password_confirmation]
    identity.password = params[:new_password].to_s
    identity.password_confirmation = params[:new_password].to_s
    identity.save
    redirect_to root_url, :notice => "Password has been changed." + person.user_id
  else
    redirect_to :back, :alert => "Password & password confirmation are not match" 
  end
end

ChangePasswordsController テスト

describe ChangePasswordsController do

  setup do
    request.env["omniauth.auth"] = OmniAuth.config.mock_auth[:identity] 
    @auth=request.env["omniauth.auth"]
  end

  it "should have edit action" do
    get :edit
    assert_response :success
  end

  it "should find person" do
     ... 
  end

  it "should find identity" do
     ... 
  end

end
4

1 に答える 1

0

where が人を見つけるかどうかのテストを書くべきではないと思います。このコンテキストでは、 update メソッドが特定のパラメーターで Person の find メソッドを呼び出すかどうかを確認するだけでよいでしょう。次のようなものを使用します。

Person.should_receive(:find).with(id: <the userid you've setup for the test>) {[Person.new]}

それも最初にチェックできるはずですが、覚えていません。チェーンスタブと関係があります。

于 2013-03-29T11:45:51.867 に答える