require 'spec_helper'
describe Api::CatsController, 'GET index' do
it 'returns a list of cats' do
FactoryGirl.create_list(:cat, 2)
get :index
expect(response.body).to eq _your_expected_json_format_
end
end
I try to follow your suggestion and use your codes.
So I install “rspec-rails” and “factory_girl_rails”, and create “spec/controllers/cats_controller_spec.rb”.
Following are my codes.
spec/controllers/cats_controller_spec.rb
require 'spec_helper'
describe Api::CatsController, 'GET index' do
it 'returns a list of cats' do
FactoryGirl.create_list(:cat, 1)
get :index
expect(response.body).to eq 'Persian'
end
end
Gemfile
group :development, :test do
gem 'rspec-rails', '~> 2.0'
end
group :test do
gem 'factory_girl_rails'
end
I add following codes into “spec/spec_helper.rb”
config.include FactoryGirl::Syntax::Methods
spec/factories.rb
FactoryGirl.define do
factory :cat do
name 'Persian'
end
end
When I execute “rspec spec/controllers/cats_controller_spec.rb”
I got following error message.
F
Failures:
1) Api::CatsController GET index returns a list of cats
Failure/Error: expect(response.body).to eq 'Persian'
expected: "Persian"
got: " "
(compared using ==)
# ./spec/controllers/cats_controller_spec.rb:9:in `block (2 levels) in <top (required)>'
Finished in 0.05409 seconds
1 example, 1 failure
Failed examples:
rspec ./spec/controllers/cats_controller_spec.rb:4 # Api::CatsController GET index returns a list of cats
Randomized with seed 23347
Its a Request spec that emulates a curl on your application.
Here is an example of a spec hitting the /api/cats endpoint:
# spec/requests/cats_api_spec.rb
require 'spec_helper'
describe 'Cat management' do
it 'returns a list of cats' do
# create a cat in the database with name Persian
FactoryGirl.create(:cat, name: 'Persian')
# make a get request to the cats index (note the format json)
get '/api/cats', format: 'json'
# parse the json into ruby
result = JSON.load(response.body)
# asert that the cat returned from the API has the name Persian
# note it's an array of hashes so i'm using ".first" on the array
expect(result.first['name']).to eq 'Persian'
end
it 'creates a cat' do
# some test data to POST to the /api/cats endpoint
end
end
This is a very simple example and you want to assert more about the returned cat. Hopefully this gets you going in the right direction.