用 Rails JSONAPI::Serializer 测试 rspec post
Testing rspec post with Rails JSONAPI::Serializer
我正在使用 jsonapi-序列化程序 gem,但在弄清楚如何使用 [=40= 测试 post 请求时遇到了一些麻烦] 和 json 有效载荷。我知道它有效,因为我可以使用 postman 并发送 json 并成功创建新对象,但我不确定为什么我无法让 rspec 测试工作.
这是 api 控制器方法:
def create
@sections = @survey.sections.all
if @sections.save
render json: serialize_model(@section), status: :created
else
render json: @section.errors, status: :unproccessable_entity
end
end
serialize_model
只是JSONAPI::Serializer.serialize
的帮手
这是我目前对该控制器的 rspec 测试:
describe 'POST #create' do
before :each do
@section_params = { section: { title: 'Section 1', position: 'top', instructions: 'fill it out' } }
post '/surveys/1/sections', @section_params.to_json, format: :json
end
it 'responds successfully with an HTTP 201 status code' do
expect(response).to be_success
expect(response).to have_http_status(201)
end
end
我尝试了几种不同的方法,但不知道如何解决这个问题。如果我用 Postman 和那个确切的 json 负载创建 post 那个 url,它会成功创建新部分。
get 请求测试工作正常,我只是不确定如何使用 rspec 和 jsonapi-serializer 处理 json 请求数据.
试试这个。用 YourApiController
代替你命名的
describe YourApiController, type: :controller do
context "#create" do
it 'responds successfully with an HTTP 201 status code' do
params = { section: { title: 'Section 1', position: 'top', instructions: 'fill it out' } }
survey = double(:survey, sections: [])
sections = double(:sections)
section = double(:section)
expect(survey).to receive(:sections).and_return(sections)
expect(sections).to receive(:all).and_return(sections)
expect(sections).to receive(:save).and_return(true)
expect(controller).to receive(:serialize_model).with(section)
post :create, params, format: :json
expect(response).to be_success
expect(response).to have_http_status(201)
expect(assigns(:sections)).to eq sections
end
end
end
我正在使用 jsonapi-序列化程序 gem,但在弄清楚如何使用 [=40= 测试 post 请求时遇到了一些麻烦] 和 json 有效载荷。我知道它有效,因为我可以使用 postman 并发送 json 并成功创建新对象,但我不确定为什么我无法让 rspec 测试工作.
这是 api 控制器方法:
def create
@sections = @survey.sections.all
if @sections.save
render json: serialize_model(@section), status: :created
else
render json: @section.errors, status: :unproccessable_entity
end
end
serialize_model
只是JSONAPI::Serializer.serialize
的帮手
这是我目前对该控制器的 rspec 测试:
describe 'POST #create' do
before :each do
@section_params = { section: { title: 'Section 1', position: 'top', instructions: 'fill it out' } }
post '/surveys/1/sections', @section_params.to_json, format: :json
end
it 'responds successfully with an HTTP 201 status code' do
expect(response).to be_success
expect(response).to have_http_status(201)
end
end
我尝试了几种不同的方法,但不知道如何解决这个问题。如果我用 Postman 和那个确切的 json 负载创建 post 那个 url,它会成功创建新部分。
get 请求测试工作正常,我只是不确定如何使用 rspec 和 jsonapi-serializer 处理 json 请求数据.
试试这个。用 YourApiController
代替你命名的
describe YourApiController, type: :controller do
context "#create" do
it 'responds successfully with an HTTP 201 status code' do
params = { section: { title: 'Section 1', position: 'top', instructions: 'fill it out' } }
survey = double(:survey, sections: [])
sections = double(:sections)
section = double(:section)
expect(survey).to receive(:sections).and_return(sections)
expect(sections).to receive(:all).and_return(sections)
expect(sections).to receive(:save).and_return(true)
expect(controller).to receive(:serialize_model).with(section)
post :create, params, format: :json
expect(response).to be_success
expect(response).to have_http_status(201)
expect(assigns(:sections)).to eq sections
end
end
end