您好,我有问题测试JSONAPI与rspec和空降。获取低于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个失败
发布于 2017-01-23 18:56:04
您的接口响应没有密钥books。取而代之的是,它将响应返回为{ "data": [ ... ] }。
因此,在测试中,您需要指定expect_json('data.0', ...),而不是expect_json('books.0', ...)。
希望这能解决这个问题。
发布于 2017-01-24 00:09:11
已通过以下方式解决:
describe Api::UsersController do describe 'GET on /users‘do 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结束
https://stackoverflow.com/questions/41804221
复制相似问题