簡體   English   中英

使用rspec和Airborne進行JSONAPI測試

[英]JSONAPI testing with rspec and Airborne

您好,我在用rspec和空降測試JSONAPI時遇到問題。 GET模型位於https://i.stack.imgur.com/Cyf75.png

我以這種方式進行測試https://i.stack.imgur.com/Y9rHt.png

Rspec輸出:

失敗:1)/ contacts上的GET應該驗證類型失敗/錯誤:Expect_json('books.0',標題:'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例失敗

您的API響應沒有關鍵books 相反,它以{ "data": [ ... ] }形式返回響應。

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

希望能解決這個問題。

已經解決:

describe Api :: UsersController確實描述了'在/ users上獲取',然后才做FactoryGirl.create('user',名稱:'Jack McClure')FactoryGirl.create('user',名稱:'Johny Reaper')FactoryGirl.create( '用戶',名稱:'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

結束

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM