[英]How to test PUT/PATCH API with RSpec
I am trying to test some PUT/PATCH endpoint from my API, but my 'record_to_update' is not chaging as expect.我正在尝试从我的 API 测试一些 PUT/PATCH 端点,但我的“record_to_update”没有按预期进行。
I organize the spec as follow using Rails 5.2 and RSpec 3.8:我使用 Rails 5.2 和 RSpec 3.8 组织规范如下:
require 'rails_helper'
RSpec.describe UsersController, type: :controller do
context 'when request with no valid headers' do
...
...
...
end
context 'when resquest with valid headers' do
before do
request.accept = 'application/vnd.api+json'
request.content_type = 'application/vnd.api+json'
2.times do
create(:user)
end
end
describe 'PATCH/PUT /users/:id' do
let(:record_to_update) { create(:user) }
let(:user_params) do
{ id: first_user.id, name: 'goku', email: 'goku@bol.com' }
end
before do
put :update, params: { id: record_to_update, user: user_params }
record_to_update.reload
end
it 'should update user' do
expect(record_to_update.name).to eq('goku')
expect(response.status).to eq 200
end
end
end
end
As I said the problem here that I am facing is that 'record_to_update' is not chaging.正如我所说的,我在这里面临的问题是“record_to_update”没有更新。 I already test the same PUT/PATCH endpoint in the same API with Postman and successfully updated from there.
我已经用 Postman 在同一个 API 中测试了同一个 PUT/PATCH 端点,并从那里成功更新。 What am I doing wrong here?
我在这里做错了什么?
Thanks in advance.提前致谢。
you should move the before
block to it 'should update user'
您应该将
before
的块移动到it 'should update user'
before do
put :update, params: { id: record_to_update.id, user: user_params }
end
So it becoming所以它变成
it 'update user' do
put :update, params: { id: record_to_update, user: user_params }
expect ...
record.reload!
....
end
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.