Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

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

visit category_path('param1')

Then I want to confirm a api data will be shown in view

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

But the @data value always be null. Why? Is it necessary to do a api url access post from test code? If it is necessary, how to do? The visit method can't take other params.

like image 478
s-cho-m Avatar asked Apr 13 '15 04:04

s-cho-m


1 Answers

Capybara is not designed for testing APIs. You should go with Rspec and Airbone: https://github.com/brooklynDev/airborne

like image 185
Ricardo Nacif Avatar answered Oct 16 '22 01:10

Ricardo Nacif