s-cho-m
s-cho-m

Reputation: 1027

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.

Upvotes: 6

Views: 2255

Answers (1)

Ricardo Nacif
Ricardo Nacif

Reputation: 508

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

Upvotes: 5

Related Questions