如何在Rails中使用`it_returns_status(200)`RSpec方法?

How to use the `it_returns_status(200)` RSpec method in Rails?

我正在学习在 API 端点上实施 RSpec,并遵循本教程,https://labs.kollegorna.se/blog/2015/04/build-an-api-now/

附件github(https://github.com/vasilakisfil/rails_tutorial_api/blob/008af67e88897a5bcde714ce13d39a26ec70fba7/spec/apis/users_spec.rb)中部分测试方法为:

但是当我在自己的应用程序中尝试这些时,出现了错误。

method_missing': undefined methodit_returns_status' for RSpec::ExampleGroups::ApiV1ProjectsController::Show:Class (NoMethodError)

并且在谷歌搜索时也找不到这些方法。

有没有人以前使用过这些并且知道它们是否是附加库的一部分?或者是其他东西?我是 RSpec.

的新手
  1. 要测试状态,请使用特定的验证器而不是 it_returns_status:

    expect(response).to have_http_status(:success)
    
  2. 要测试 JSON 答案,请使用解析器并测试字段而不是属性对:

    record = JSON.parse(response.body)   
    expect(record["root"]).to eq 'users'
    expect(record["number"]).to eq 6
    

    要了解有关 JSON 测试的更多信息,请参阅 the article

  3. 如果您的意思是资源是路径,请使用路径验证器,例如:

    expect(current_path).to eq(root_path(@user))