我有以下:导轨 - 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
可能*验证*失败? –
我在这里注意到的另一件事是,发布请求应该在像https://stackoverflow.com/a/41730044/2767755这样的块内。 –
不,最后一次测试通过 – davideghz