2017-09-01 60 views
0

我有以下:导轨 - Rspec的:测试PUT更新实际更改模型属性

let(:update_request) { put "/api/v3/account/profile.json", attributes, credentials } 

describe "PUT 'update'" do 
    before { update_request } 

    context "updating normal attributes (no password)" do 
    let(:attributes) { 
     { 
     profile: { first_name: 'John', phone: '02 21231321', email: '[email protected]' } 
     } 
    } 

    it do 
     aggregate_failures do 
     # expect{response}.to change{current_user.reload.email}.to('[email protected]') 
     expect(response).to be_success 
     expect(current_user.reload.first_name).to eq('John') 
     expect(current_user.reload.phone).to eq('02 21231321') 
     expect(current_user.reload.email).to eq('[email protected]') 
     end 
    end 
    end 
... 

,在上面的代码被注释了该期望失败,出现以下消息:

Failure/Error: expect{response}.to change{current_user.reload.email}.to('[email protected]') expected result to have changed to "[email protected]", but did not change

我如何测试对控制器PUT /更新操作的调用实际上是否更改了模型属性?

编辑

如果我检查和之前失败的测试后评价current_user与binding.pry,我看到它实际上改变。测试仍然失败寿

binding.pry 
expect{response}.to change{current_user.reload.email}.to('[email protected]') 
binding.pry 
+0

可能*验证*失败? –

+0

我在这里注意到的另一件事是,发布请求应该在像https://stackoverflow.com/a/41730044/2767755这样的块内。 –

+0

不,最后一次测试通过 – davideghz

回答

0

你必须改变你的期望代码像下面利用变化,从和

expect { 
    put "/api/v3/account/profile.json", attributes, credentials 
}.to change{current_user.reload.email}.to('[email protected]') 

其实这是很好的地方请求语句(GET, PUT,POST和DELETE)里面的它阻止,所以我们应该能够使用rspec expec的功能。

+0

我得到了相同的预期结果,改为“[email protected]”,但没有改变失败 – davideghz