2010-07-22 2 views
0

간단한 비밀번호 재설정 작업을 테스트 중이며 lambda에 대한 RSpec의 "변경"일치자를 원합니다. 그러나이 컨트롤러 동작에는 작동하지 않습니다. Matcher 없이는 모든 것이 잘 작동합니다. 다음은 사양입니다.람다가 있거나없는 컨트롤러 스펙 "이 변경되어야 함"

describe "#update" do 
it "Updates the password and resets the token" do 
    @user = Factory :user 
    getter = lambda{ 
    get :edit, :id => @user.perishable_token, :user => {:password_confirmation => "new_password", 
                 :password => "new_password"} 
    @user.reload 
    } 
    getter.should change(@user, :password) 
    getter.should change(@user, :perishable_token) 
end 

it "Updates the password and resets the token" do 
    @user = Factory :user 
    old_password = @user.password 
    old_token = @user.perishable_token 
    get :edit, :id => @user.perishable_token, :user => {:password_confirmation => "new_password", 
                 :password => "new_password"}   
    @user.reload.password.should != old_password 
    @user.perishable_token.should != old_token 
end 
end 

두 번째 it 블록은 작동하지만 첫 번째 블록은 작동하지 않습니다. 나는 람다 내부에 값을 인쇄하려했지만 실제로 변경되지 않았습니다.

이 문제에 대한 아이디어를 제공해 주셔서 감사합니다.

답변

0

그래서 Change-matcher가 proc를 호출합니다. 그래서 그게 문제가 아니 었습니다. 그러나 나는 Edit가 아니라 Update라고 부르고 있었다. 그래서 아무 것도 바뀌지 않았어. 또한 암호은 암호가 설정된 사용자 개체 및 일반 텍스트 암호가 저장되지 않아 DB에서 검색되지 않는 사용자 개체에만 있습니다.

나는 그것이 바보 같은 오류로 밝혀 질 것이라고 생각
describe "#update" do 
it "Updates the password and resets the token" do 
    @user = Factory.create :user 
    getter = lambda{ 
    post :update, :id => @user.perishable_token, :user => {:password_confirmation => "new_password", 
                  :password => "new_password"} 
    @user.reload 
    } 
    getter.should change(@user, :crypted_password) 
    getter.should change(@user, :perishable_token) 
end 
end 

하지만이 하나 ...

0

첫 번째 예제에서 람다를 실제로 실행하려면 call을 사용해야합니다. 람다를 getter에 할당하지만 실제로는 람다를 실행하고 결과를 얻으려면 getter.call을 수행하지 마십시오.

+0

람다가 이미 실행 얻을 것으로 보인다 다음은 현재 작업 코드입니다. 람다 정의와 기대 사이에 "getter.call"을 시도했지만 아무 것도 변경되지 않았습니다. 그런 다음 "getter.call.should change (@user, : password)"를 시도했지만 사용자에게 'call'메서드가 없음을 알리는 오류가 발생했습니다. 그래서 나는 lamda가 @ user.reload라는 마지막 문장을 반환했다고 결론을 내렸다. 그것은 실행 된 lamda의 끝에서 print 문을 추가함으로써 확인되었고 오류 메시지는 nil에 대해 정의되지 않은 메서드 인 'call'로 변경되었습니다. – ajmurmann

관련 문제