2015-12-29 95 views
-1

我正在編寫更新密碼的測試! 這裏是我的代碼:使用Rspec更新密碼測試

def update_password 
if @current_user.update(user_params) 
    # Sign in the user by passing validation in case their password changed 
    # sign_in @user, :bypass => true 
    json_success("Password successfully updated") 
else 
    json_not_found("#{ @user.errors.full_messages.to_sentence}") 
end 

和我的測試是:

context 'update password' do 
    def update_password 
    patch :update_password, { password: 'password', 
           password_confirmation: 'password' } 
    end 

    it 'should update user password on user request' do 
    expect(user.valid_password?('password')).to eq(false) 
    update_password 
    user.reload 
    puts response.body.inspect 
    expect(user.valid_password?('password')).to eq(true) 
    end 
end 

,但我不斷收到錯誤:

1) Api::V2::UsersController patch #update_password update password should update user password on user request 
Failure/Error: expect(user.valid_password?('password')).to eq(true) 

    expected: true 
     got: false 

    (compared using ==) 

還有一個追問如何會傳遞一個帶有驗證令牌的json頭文件:謝謝

回答

0

你的規格和控制器應該更接近這個,這應該是幫助你通過這個錯誤。 可能有多種其他的錯誤,我不能@user或@current_user看到

# app/controllers/some_controller.rb 
def update_password 
    if @current_user.update(user_params) 
    render json: "Password successfully updated" 
    else 
    render json: @user.errors.full_messages.to_sentence 
    end 
end 

# spec/controllers/some_controller.rb 
describe SomeController, type: :controller do 
    it '#update_password' do 
    params = double(:params, password: 'password', password_confirmation: 'password') 
    current_user = double(:current_user) 
    expect(current_user).to receive(:update).with(params).and_return(true) 
    patch :update_password, params 
    expect(response.body).to eq "Password successfully updated" 
    end 
end 

one more questing how will a pass a json header with authenticate token in it: thanks

^^應該是不同的,所以質疑