你好我用rspec和空中测试JSONAPI有问题。 GET模型如下 https://i.stack.imgur.com/Cyf75.png
我用这种方式测试https://i.stack.imgur.com/Y9rHt.png
Rspec输出:
失败: 1)GET on / contacts应验证类型 失败/错误: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次失败
答案 0 :(得分:0)
您的API响应没有密钥books
。相反,它将响应返回为{ "data": [ ... ] }
。
因此,在测试中,您需要指定expect_json('data.0', ...)
,而不是expect_json('books.0', ...)
。
希望能解决这个问题。
答案 1 :(得分:0)
已经解决:
描述Api :: UsersController 描述&#39; GET on / users&#39;做 之前做 FactoryGirl.create(&#39;用户&#39;,名称:&#39; Jack McClure&#39;) FactoryGirl.create(&#39;用户&#39;,名称:&#39; Johny Reaper&#39;) FactoryGirl.create(&#39;用户&#39;,名称:&#39; Niko Bellic&#39;) 端
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
端