【山竹记账后端】13.超快速实现后端接口
大纲链接 §
[toc]
基本步骤
- 创建
model,运行db:migrate - 创建
controller - 写测试
- 写代码
- 写文档(测试代码)
1. 快速实现读取API ⇧
实现
/api/v1/tags
创建 tag 的 model
|
|
- 运行命令
bin/rails g model tag前,需要看下概要设计文档数据库设计中tag表中的字段:user:references识别为引用到user表,自动在表中生成字段user_id并匹配类型bigintname:stringsign:stringdeleted_at:datetimecreated_at:datetimeupdated_at:datetime
- 确认然后再运行
bin/rails g model tag user:references name:string sign:string deleted_at:datetime 生成以下文件:
1 2 3 4 5 6bin/rails g model tag user:references name:string sign:string deleted_at:datetime invoke active_record create db/migrate/20261128130348_create_tags.rb create app/models/tag.rb invoke rspec create spec/models/tag_spec.rb
打开
db/migrate/xxxxxxxxxxx_create_tags.rb需要在迁移前补充更多详细配置,修改如下:
|
|
t.references :user, null: false, foreign_key: false表示引用到user表,不能为空,不使用外键(不方便测试)t.string :name, null: false不能为空,如果此处不写null: false,那么需要在ruby验证器中添加不能为空的验证目前
db/schema.rb中还没有这个表需要创建,运行bin/rails db:migrate1 2 3 4== 20261128130348 CreateTags: migrating ======================================= -- create_table(:tags) -> 0.0295s == 20261128130348 CreateTags: migrated (0.0296s) ==============================可以看到
db/schema.rb中已经生成了tag表之前写的引用
t.references :user, null: false, foreign_key: false- 自动变为
t.bigint "user_id", null: false的引用"user_id"类型t.bigint比较智能
- 自动变为
db/schema.rb
|
|
app/models/tag.rb
|
|
belongs_to :user于数据表关联,暂时忽略spec/models/tag_spec.rb中不写测试,暂时不测model,可以删掉
创建 tag 的 controller ⇧
运行命令
bin/rails g controller api/v1/tags_controller1 2 3create app/controllers/api/v1/tags_controller.rb invoke rspec create spec/requests/api/v1/tags_spec.rb
spec/requests/api/v1/tags_spec.rb
|
|
app/controllers/api/v1/tags_controller.rb
|
|
写 tag 的测试用例 ⇧
添加未登录获取标签测试用例
spec/requests/api/v1/tags_spec.rb
|
|
- 运行
rspec spec/requests/api/v1/tags_spec.rb成功- 是因为之前中间件
auto_jwt.rb中只要不在白名单中的请求路径,请求头jwt为空,即返回401
- 是因为之前中间件
完成测试用例
spec/requests/api/v1/tags_spec.rb测试分页
|
|
- 运行测试用例
rspec spec/requests/api/v1/tags_spec.rb:9 - 发现报错
null value in column "sign" of relation "tags" violates not-null constraint 补充
sign属性Tag.create name: "tag#{i}", user_id: user.id, sign: '😄'1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17rspec spec/requests/api/v1/tags_spec.rb:9 Run options: include {:locations=>{"./spec/requests/api/v1/tags_spec.rb"=>[9]}} F Failures: 1) Api::V1::Tags 获取标签 已登录获取标签(分页) Failure/Error: expect(response).to have_http_status :ok expected the response to have status code :ok (200) but it was :not_found (404) # ./spec/requests/api/v1/tags_spec.rb:16:in `block (3 levels) in <top (required)>' Finished in 0.15225 seconds (files took 0.68188 seconds to load) 1 example, 1 failure Failed examples: rspec ./spec/requests/api/v1/tags_spec.rb:9 # Api::V1::Tags 获取标签 已登录获取标签(分页)现在去实现
controller
实现 tag 的 controller ⇧
添加一个 index 方法
app/controllers/api/v1/tags_controller.rb
|
|
运行测试用例
rspec spec/requests/api/v1/tags_spec.rb:9会返回1 2 3 4 5 6 7 8 9 10 11 12 13Failures: 1) Api::V1::Tags 获取标签 已登录获取标签(分页) Failure/Error: expect(response).to have_http_status :ok expected the response to have status code :ok (200) but it was :no_content (204) # ./spec/requests/api/v1/tags_spec.rb:16:in `block (3 levels) in <top (required)>' Finished in 0.06641 seconds (files took 1.08 seconds to load) 1 example, 1 failure Failed examples: rspec ./spec/requests/api/v1/tags_spec.rb:9 # Api::V1::Tags 获取标签 已登录获取标签(分页)空方法默认返回
204 No Content
app/controllers/api/v1/tags_controller.rb
|
|
运行测试用例
rspec spec/requests/api/v1/tags_spec.rb:9会返回1 2 3 4 5 6 7 8 9 10 11 12 13Failures: 1) Api::V1::Tags 获取标签 已登录获取标签(分页) Failure/Error: expect(response).to have_http_status :ok expected the response to have status code :ok (200) but it was 444 # ./spec/requests/api/v1/tags_spec.rb:16:in `block (3 levels) in <top (required)>' Finished in 0.08136 seconds (files took 1.1 seconds to load) 1 example, 1 failure Failed examples: rspec ./spec/requests/api/v1/tags_spec.rb:9 # Api::V1::Tags 获取标签 已登录获取标签(分页)
app/controllers/api/v1/tags_controller.rb
|
|
运行测试用例
rspec spec/requests/api/v1/tags_spec.rb:9会返回1 2 3 4 5Run options: include {:locations=>{"./spec/requests/api/v1/tags_spec.rb"=>[9]}} . Finished in 0.06543 seconds (files took 0.66441 seconds to load) 1 example, 0 failures虽然测试用例成功了,但没有区分是否当前用户创建的标签
继续修改测试用例
spec/requests/api/v1/tags_spec.rb
|
|
- 数据库一般都包含不同用户创建的所有标签,请求只能筛选出当前用户的所有标签
再次运行
rspec spec/requests/api/v1/tags_spec.rb:9报错1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21rspec spec/requests/api/v1/tags_spec.rb:9 Run options: include {:locations=>{"./spec/requests/api/v1/tags_spec.rb"=>[9]}} F Failures: 1) Api::V1::Tags 获取标签 已登录获取标签(分页) Failure/Error: expect(json['resources'].size).to eq 1 expected: 1 got: 10 (compared using ==) # ./spec/requests/api/v1/tags_spec.rb:25:in `block (3 levels) in <top (required)>' Finished in 0.09378 seconds (files took 0.69141 seconds to load) 1 example, 1 failure Failed examples: rspec ./spec/requests/api/v1/tags_spec.rb:9 # Api::V1::Tags 获取标签 已登录获取标签(分页)期待第二页的数据为1,但实际为10
修改
controller实现,筛选当前用户的标签
app/controllers/api/v1/tags_controller.rb
|
|
- 再次运行
rspec spec/requests/api/v1/tags_spec.rb:9就成功了
写tags文档(测试代码) ⇧
新建文件
touch spec/acceptance/tags_spec.rb
|
|
- 运行
bin/rake docs:generate测试通过生成文档 - 运行
pnpx http-server doc/api/.查看文档,发现缺少分页信息pager字段
修改
app/controllers/api/v1/tags_controller.rb
|
|
- 运行
bin/rake docs:generate测试通过生成文档 - 运行
pnpx http-server doc/api/.查看文档
‘post /api/v1/tags’
创建已创建model,运行db:migrate创建已创建controller- 写测试
- 写代码
- 写文档(测试代码)
2. 快速实现创建API ⇧
创建标签接口功能测试 ⇧
``
|
|
实现创建标签接口功能 ⇧
``
|
|
完成创建标签接口文档 ⇧
``
|
|
3. 快速实现更新API ⇧
更新标签接口功能测试 ⇧
``
|
|
实现更新标签接口功能 ⇧
``
|
|
完成更新标签接口文档 ⇧
``
|
|
4. 快速实现删除API ⇧
删除标签接口功能测试 ⇧
``
|
|
实现删除标签接口功能 ⇧
``
|
|
完成删除标签接口文档 ⇧
``
|
|
5. 快速实现单个读取API ⇧
``
|
|
6. $3 ⇧
``
|
|
7. 可能的报错信息 ⇧
密钥相关报错
- 拿到代码后,可能需要重新创建密钥:
https://docs.qq.com/doc/DU3ByQXZWbFpQRHND。 - 这一步是可选的,但如果遇到
ActiveSupport::MessageEncryptor::InvalidMessage报错,那么你就必须重新创建密钥了。
参考文章
相关文章
- 无
- 作者: Joel
- 文章链接:
- 版权声明
- 非自由转载-非商用-非衍生-保持署名