我有REST API
,例如:我已经创建了REST用户,创建用户的终点是转到"localhost:3000/users"POST方法。
所以参数的模式应该是这样的:
:user => {:name => "apple", :address => "heaven"}
我的问题是:
如何创建上面的模式,然后我测试它到端点POST方法?
我已经用postman
客户端进行了测试,但由于我的模式不正确(错过root : user
)而失败
感谢
控制器测试将捕获您所描述的错误类型,并且比端到端测试更有效。RSpec:中的示例
it 'creates a new user' do
post :create, user: {name: 'apple', address: 'heaven'}, format: 'json'
assert_response :success
expect(response).to render_template(:create)
expect(assigns(:user).name).to eq('apple')
expect(assigns(:user).address).to eq('heaven')
end
我已经使用curl
(在代码中)对API进行了端到端测试。法拉第宝石可能也会起作用。
对于curl
请求,您需要为数据传递-X POST
参数和-d
参数。-d
可以具有以下格式:
-d "user[name]=apple" -d "user[address]=heaven"
-d {"user": {"name": "apple", "address": "heaven"}}
您也可以使用-H
传递标头。
总计:
curl http://your_url -X POST -H "Content-type: application/json" -d {"user": {"name": "apple", "address": "heaven"}}