Last active
August 29, 2015 13:56
-
-
Save kelso/9185463 to your computer and use it in GitHub Desktop.
Testing controller with RSpec - sample.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'spec_helper' | |
describe CampaignsController do | |
let(:user){ create(:admin_user) } | |
before(:each) do | |
sign_in user | |
end | |
describe "GET #index" do | |
let!(:campaign){ create(:campaign) } | |
it "renders the :index view" do | |
get :index | |
response.should render_template :index | |
end | |
it "populates an array of campaigns" do | |
get :index | |
assigns(:campaigns).should eq([campaign]) | |
end | |
end | |
describe "GET #show" do | |
let(:campaign){ create(:campaign) } | |
it "renders the :show view" do | |
get :show, :id => campaign.id | |
response.should render_template :show | |
end | |
it "assigns requested campaign to @campaign" do | |
get :show, :id => campaign.id | |
assigns(:campaign).should eq(campaign) | |
end | |
end | |
describe "GET #new" do | |
it "renders the :new view" do | |
get :new | |
response.should render_template :new | |
end | |
it "assigns new campaign to @campaign" do | |
get :new | |
assigns(:campaign).should be_new_record | |
end | |
end | |
describe "POST #create" do | |
context "with valid attributes" do | |
it "creates a new campaign" do | |
expect{ | |
post :create, :campaign => attributes_for(:campaign) | |
}.to change{Campaign.count}.by(1) | |
end | |
it "redirects to campaigns listing" do | |
post :create, :campaign => attributes_for(:campaign) | |
response.should redirect_to :campaigns | |
end | |
end | |
context "with invalid attributes" do | |
it "does not save the new campaign" do | |
expect{ | |
post :create, :campaign => attributes_for(:invalid_campaign) | |
}.to_not change{Campaign.count} | |
end | |
it "re-renders the new action" do | |
post :create, :campaign => attributes_for(:invalid_campaign) | |
response.should render_template :new | |
end | |
end | |
end | |
describe "GET #edit" do | |
let(:campaign){ create(:campaign) } | |
it "assigns requested campaign to @campaign" do | |
get :edit, :id => campaign.id | |
assigns(:campaign).should eq(campaign) | |
end | |
it "renders the :edit view" do | |
get :edit, :id => campaign.id | |
response.should render_template :edit | |
end | |
end | |
describe "PUT #update" do | |
let(:campaign){ create(:campaign) } | |
context "with valid attributes" do | |
it "locates the requested campaign" do | |
put :update, :id => campaign.id, :campaign => attributes_for(:campaign) | |
assigns(:campaign).should eq(campaign) | |
end | |
it "changes campaign attributes" do | |
put :update, :id => campaign.id, :campaign => attributes_for(:campaign, :name => "Updated name") | |
campaign.reload | |
campaign.name.should eq("Updated name") | |
end | |
it "redirects to campaigns listing" do | |
put :update, :id => campaign.id, :campaign => attributes_for(:campaign) | |
response.should redirect_to :campaigns | |
end | |
end | |
context "with invalid attributes" do | |
it "locates the requested campaign" do | |
put :update, :id => campaign.id, :campaign => attributes_for(:invalid_campaign) | |
assigns(:campaign).should eq(campaign) | |
end | |
it "does not change campaign's attributes" do | |
put :update, :id => campaign.id, :campaign => attributes_for(:campaign, :name => "") | |
campaign.reload | |
campaign.name.should eq("MyCampaign") | |
campaign.name.should_not eq("Updated name") | |
end | |
it "re-renders the edit action" do | |
put :update, :id => campaign.id, :campaign => attributes_for(:invalid_campaign) | |
response.should render_template :edit | |
end | |
end | |
end | |
describe "DELETE #destroy" do | |
let!(:campaign){ create(:campaign) } | |
it "deletes the campaign" do | |
expect{ | |
delete :destroy, :id => campaign.id | |
}.to change{Campaign.count}.by(-1) | |
end | |
it "redirects campaigns listing" do | |
delete :destroy, :id => campaign.id | |
response.should redirect_to :campaigns | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment