【问题标题】:JSONAPI testing with rspec and Airborne使用 rspec 和 Airborne 进行 JSONAPI 测试
【发布时间】:2017-01-24 00:09:10
【问题描述】:

您好,我在使用 rspec 和机载测试 JSONAPI 时遇到问题。 获取以下模型 https://i.stack.imgur.com/Cyf75.png

我用这种方式测试https://i.stack.imgur.com/Y9rHt.png

Rspec 输出:

失败: 1) /contacts 上的 GET 应该验证类型 失败/错误:expect_json('books.0', title: 'The Great Escape')

 Airborne::PathError:
   Expected NilClass
   to be an object with property 0
 # /home/robert/.rvm/gems/ruby-2.4.0/gems/airborne-0.2.5/lib/airborne/path_matcher.rb:21:in `rescue in block in get_by_path'
 # /home/robert/.rvm/gems/ruby-2.4.0/gems/airborne-0.2.5/lib/airborne/path_matcher.rb:18:in `block in get_by_path'
 # /home/robert/.rvm/gems/ruby-2.4.0/gems/airborne-0.2.5/lib/airborne/path_matcher.rb:9:in `each'
 # /home/robert/.rvm/gems/ruby-2.4.0/gems/airborne-0.2.5/lib/airborne/path_matcher.rb:9:in `each_with_index'
 # /home/robert/.rvm/gems/ruby-2.4.0/gems/airborne-0.2.5/lib/airborne/path_matcher.rb:9:in `get_by_path'
 # /home/robert/.rvm/gems/ruby-2.4.0/gems/airborne-0.2.5/lib/airborne/request_expectations.rb:137:in `call_with_path'
 # /home/robert/.rvm/gems/ruby-2.4.0/gems/airborne-0.2.5/lib/airborne/request_expectations.rb:18:in `expect_json'
 # ./book_resource.rb:10:in `block (2 levels) in <top (required)>'
 # ------------------
 # --- Caused by: ---
 # NoMethodError:
 #   undefined method `[]' for nil:NilClass
 #   /home/robert/.rvm/gems/ruby-2.4.0/gems/airborne-0.2.5/lib/airborne/path_matcher.rb:57:in `process_json'

在 0.03121 秒内完成(文件加载耗时 0.17681 秒) 1 个示例,1 个失败

【问题讨论】:

  • 请张贴代码而不是截图链接。
  • 我做不到,因为rep太低了

标签: ruby-on-rails ruby testing rspec


【解决方案1】:

您的 API 响应没有密钥 books。相反,它以{ "data": [ ... ] } 的形式返回响应。

因此,在您的测试中,您需要指定expect_json('data.0', ...),而不是expect_json('books.0', ...)

希望能解决这个问题。

【讨论】:

  • 1) /users 上的 GET 应该返回有效类型失败/错误:expect_json('data.1', name: "Jack McClure") Airborne::PathError: Expected NilClass to be an object with property 1 1) /users 上的 GET 应该返回有效类型失败/错误:expect_json('data.1', name: "Jack McClure") # --- Caused by: --- # NoMethodError: # undefined method []' for nil:NilClass # /home/robert/.rvm/gems/ruby-2.4.0/gems/airborne-0.2.5/lib/airborne/path_matcher.rb:57:in process_json '
  • 在 2017-01-23 12:18:20 +0100 ActiveRecord::SchemaMigration Load (0.2ms) 开始 GET "/api/users" for 127.0.0.1 选择 "schema_migrations".* FROM " schema_migrations" 由 Api::UsersController#index 作为 XML 参数处理:{"user"=>{}} 过滤器链停止为:ensure_valid_accept_media_type 呈现或重定向完成 406 Not Acceptable in 201ms (Views: 0.2ms | ActiveRecord: 0.9ms)
  • @RobsonRbn 我知道您的声望很低,但您可以使用PasteBin 等第三方工具向我展示您的代码。我无法理解 cmets 中的日志。
  • 很抱歉我忘记了这个 GET 请求:pastebin.com/Bf4YB30n Rspec 运行:pastebin.com/2F2mGFBd
【解决方案2】:

已经解决了:

描述 Api::UsersController 做 描述 'GET on /users' 做 在做之前 FactoryGirl.create('user', name: 'Jack McClure') FactoryGirl.create('user', name: 'Johny Reaper') FactoryGirl.create('user', name: 'Niko Bellic') 结束

it 'should return valid user' do
  get :index, format: 'json'
  expect_json('data.0.attributes', name: "Jack McClure")
  expect_json('data.2.attributes', name: 'Niko Bellic')
end

结束

【讨论】:

    猜你喜欢
    • 2016-05-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-11
    • 2021-11-15
    • 1970-01-01
    相关资源
    最近更新 更多