2013-03-20 57 views
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 

回答

0

我不認爲那裏是否找到一個人或不應該寫一個測試。在這種情況下,您最好檢查update方法是否使用給定參數調用Person上的find方法。使用像這樣的東西:

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

它應該也能夠先檢查,但我不記得,與chainstubbing有關。

相關問題