API 设计实战指南
1. 项目创建 API 测试与实现
在 API 开发过程中,首先要确保项目创建功能的正确性。运行 bin/rspec spec/api/v1/projects_spec.rb 后,若显示 “3 examples, 0 failures”,说明已有部分测试通过。接下来,需要编写一个测试,以验证当传入一个没有名称的项目时,会返回 422 状态码和相应的错误信息。在 spec/api/v1/projects_spec.rb 中添加如下测试代码:
it "unsuccessful JSON" do
post "#{url}.json", :token => token,
:project => {}
last_response.status.should eql(422)
errors = {"name" => ["can't be blank"]}.to_json
last_response.body.should eql(errors)
end
再次运行测试,若显示 “4 examples, 0 failures”,则表明该测试通过。此时,用户可以通过 API 创建项目,接着进行提交操作:
git add .
git commit -m "Added API to create projects"
git push
2. 限制项目创建权限为管理员
在实际
超级会员免费看
订阅专栏 解锁全文
16万+

被折叠的 条评论
为什么被折叠?



