简体   繁体   English

如何在rails上使用rspec和capybara测试API?

[英]How to test a API with rspec and capybara on rails?

I have created a controller that can read api data by a special url. 我创建了一个控制器,可以通过特殊的URL读取api数据。

def category
  @data = get_api_data(param1)
end

def get_api_data(param1)
  "http://my_api_url/param1=#{param1}"
end

After I create a view, I can see the result from browser. 创建视图后,我可以看到浏览器的结果。

If I use rspec + capybara to do the feature test 如果我使用rspec + capybara进行功能测试

visit category_path('param1')

Then I want to confirm a api data will be shown in view 然后我想确认api数据将在视图中显示

expect(page).to have_field('name', with: 'aaa')

But the @data value always be null. @data值始终为null。 Why? 为什么? Is it necessary to do a api url access post from test code? 是否有必要从测试代码中获取api url访问帖? If it is necessary, how to do? 如果有必要,该怎么办? The visit method can't take other params. visit方法不能采取其他参数。

Capybara is not designed for testing APIs. Capybara不是为测试API而设计的。 You should go with Rspec and Airbone: https://github.com/brooklynDev/airborne 你应该选择Rspec和Airbone: https//github.com/brooklynDev/airborne

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM