patternrubyrailsMinor
RSpec integration tests for a simple Rails API
Viewed 0 times
simpletestsapirailsforintegrationrspec
Problem
The Model is simple: a
I'm just trying to get a handle on TDD for what will expand into a much more robust API. Here is my first stab at the integration/controller specs for REST actions on the API.
I haven't seen a lot of concrete examples of RSpec and API tests, so feedback is very welcome.
I'm using Rabl in my views for rendering the JSON Responses.
```
describe Api::V1::PlayersController do
render_views
before do
@player1 = FactoryGirl.create(:player, first_name: "Joe", last_name: "Smith", team_id: 1)
@player2 = FactoryGirl.create(:player, first_name: "Bob", last_name: "Jones", team_id: 2)
@player3 = FactoryGirl.create(:player, first_name: "Peter", last_name: "Wilson", team_id: 3)
end
describe "#index" do
before do
get :index, :format => :json
end
it "should retrieve status code of 200" do
response.response_code.should == 200
end
it "should retrieve license header" do
response.header["X-LS-License"].should == "All Rights Reserved"
end
it "should retrieve application name header" do
response.header["X-LS-Application"].should == "league-server"
end
it "should retrieve records-returned header" do
response.header["X-LS-Records-Returned"].should be_present
end
it "should retrieve a content-type of json" do
response.header['Content-Type'].should include 'application/json'
end
it "should retrieve list of players" do
players = Player.all
players.count.should == 3
response.body.should include(@player1.id.to_s)
response.body.should include(@player2.id.to_s)
response.body.should include(@player3.id.to_s)
response.body.should include('Joe Smith')
response.body.should include('Bob Jones')
response.body.should include('Peter Wilson')
end
end
describe "#show" do
before do
get :show, id: @player1.id, :format => :
Player class with three attributes: first_name, last_name, and team_id.I'm just trying to get a handle on TDD for what will expand into a much more robust API. Here is my first stab at the integration/controller specs for REST actions on the API.
I haven't seen a lot of concrete examples of RSpec and API tests, so feedback is very welcome.
I'm using Rabl in my views for rendering the JSON Responses.
```
describe Api::V1::PlayersController do
render_views
before do
@player1 = FactoryGirl.create(:player, first_name: "Joe", last_name: "Smith", team_id: 1)
@player2 = FactoryGirl.create(:player, first_name: "Bob", last_name: "Jones", team_id: 2)
@player3 = FactoryGirl.create(:player, first_name: "Peter", last_name: "Wilson", team_id: 3)
end
describe "#index" do
before do
get :index, :format => :json
end
it "should retrieve status code of 200" do
response.response_code.should == 200
end
it "should retrieve license header" do
response.header["X-LS-License"].should == "All Rights Reserved"
end
it "should retrieve application name header" do
response.header["X-LS-Application"].should == "league-server"
end
it "should retrieve records-returned header" do
response.header["X-LS-Records-Returned"].should be_present
end
it "should retrieve a content-type of json" do
response.header['Content-Type'].should include 'application/json'
end
it "should retrieve list of players" do
players = Player.all
players.count.should == 3
response.body.should include(@player1.id.to_s)
response.body.should include(@player2.id.to_s)
response.body.should include(@player3.id.to_s)
response.body.should include('Joe Smith')
response.body.should include('Bob Jones')
response.body.should include('Peter Wilson')
end
end
describe "#show" do
before do
get :show, id: @player1.id, :format => :
Solution
You might consider replacing this:
with
I find that very simple code checks are better done with the second form to eliminate duplication when you're reading the test.
It's helpful to review this occasionally. Something good is added frequently.
Quote from https://github.com/rspec/rspec-expectations
One-liners
The one-liner syntax supported by rspec-core uses should even when
config.syntax = :expect. It reads better than the alternative, and
does not require a global monkey patch:
describe User do it { should validate_presence_of :email } end
This is another helpful reference: https://www.relishapp.com/rspec/rspec-rails/v/2-13/docs/controller-specs
it "should retrieve status code of 200" do
response.response_code.should == 200
endwith
it { response.response_code.should == 200 }I find that very simple code checks are better done with the second form to eliminate duplication when you're reading the test.
It's helpful to review this occasionally. Something good is added frequently.
Quote from https://github.com/rspec/rspec-expectations
One-liners
The one-liner syntax supported by rspec-core uses should even when
config.syntax = :expect. It reads better than the alternative, and
does not require a global monkey patch:
describe User do it { should validate_presence_of :email } end
This is another helpful reference: https://www.relishapp.com/rspec/rspec-rails/v/2-13/docs/controller-specs
Code Snippets
it "should retrieve status code of 200" do
response.response_code.should == 200
endit { response.response_code.should == 200 }Context
StackExchange Code Review Q#17860, answer score: 3
Revisions (0)
No revisions yet.