Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PeterBloom/8660592 to your computer and use it in GitHub Desktop.
Save PeterBloom/8660592 to your computer and use it in GitHub Desktop.
require 'spec_helper'
describe GamesController do
describe 'GET#index' do
before do
@fake_games = [ double('game1'), double('game2'), double('game3') ]
end
it 'gets all games' do
Game.should_receive(:all).and_return(@fake_games)
expect(@fake_games.count).to eq(3)
get :index
end
it 'assigns all games for rendering' do
Game.stub(:all).and_return(@fake_games)
get :index
expect(assigns :games).to eq(@fake_games)
end
it 'responds successfully' do
expect(response).to be_success
end
it 'renders the All Games template' do
Game.stub(:all).and_return(@fake_games)
get :index
expect(response).to render_template :index
end
end
describe 'GET#new' do
it 'builds a new game to provide initial values to @game' do
@fake_game = FactoryGirl.build(:game)
get :new
@fake_game.title.should eq('A Fake Title')
end
it 'makes new game available to New Game template' do
@fake_game = double('fake_game')
Game.stub(:new).and_return(@fake_game)
get :new
expect(assigns :games).to eq(@fake_game)
end
it 'responds successfully' do
expect(response).to be_success
end
it 'renders the New Game template' do
get :new
expect(response).to render_template :new
end
end
describe 'GET#show' do
before do
@fake_game = FactoryGirl.create(:game)
get :show, id: @fake_game.id
end
it 'finds game by id' do
Game.stub find: @fake_game
get :show, id: @fake_game
expect(Game).to have_received(:find).with(@fake_game.id.to_s)
end
it 'assigns the requested game to @game' do
expect(assigns :game).to eq(@fake_game)
end
it 'responds successfully' do
expect(response).to be_success
end
it 'renders the #show view' do
expect(response).to render_template :show
end
end
describe 'GET#edit' do
before do
@fake_game = FactoryGirl.create(:game)
get :edit, id: @fake_game.id
end
it 'finds game by id' do
Game.stub find: @fake_game
get :edit, id: @fake_game
expect(Game).to have_received(:find).with(@fake_game.id.to_s)
end
it 'assigns the requested game to @game' do
expect(assigns :game).to eq(@fake_game)
end
it 'responds successfully' do
expect(response).to be_success
end
it 'renders the #edit view' do
expect(response).to render_template :edit
end
end
describe 'POST#create' do
context 'POST#create game saves successfully' do
it 'creates a new game' do
@fake_game = mock_model(Game, :save => true)
Game.stub(:create).and_return(@fake_game) #explicit return syntax. Compare to 'POST#create game fails to save'
post :create, :game => { :title => 'History of the Game Part 1' }
end
it 'sets a flash[:notice] message' do
Game.stub(:create).and_return(FactoryGirl.build(:game))
post :create, :game => { :title => 'History of the Game Part 1' }
expect(flash[:notice]).not_to be_empty
end
it 'sets correct flash[:notice] message' do
@fake_game = FactoryGirl.build(:game)
Game.stub(:create).and_return(@fake_game)
post :create, :game => { :title => @fake_game.title }
expect(flash[:notice]).to eq("#{@fake_game.title} was successfully created.")
end
it 'redirects to the page of newly created game' do
post :create, game: FactoryGirl.attributes_for(:game)
expect(response).to redirect_to Game.last
end
it 'makes newly created game available to game template' do
@fake_game = FactoryGirl.build(:game)
Game.stub(:create).and_return(@fake_game)
post :create, :game => @fake_game.attributes
expect(assigns :game).to eq(@fake_game)
end
end
context 'POST#create game fails to save' do
it 'does not create a game' do
@fake_game = mock_model(Game, :save => false)
Game.stub(:create) {@fake_game} #returns @fake_game, different syntax. Compare to 'POST#create game saves successfully'
post :create, :game => { :title => 'History of the Game Part 1' }
end
it 'makes the attempted created game available to game template' do
expect(assigns :game).to eq(@fake_game)
end
it 'renders the #new template' do
@fake_game = mock_model(Game, :save => false)
Game.stub(:create) {@fake_game}
post :create, :game => { :title => 'History of the Game Part 1' }
expect(response).to render_template :new
end
end
end
describe 'PUT#update' do
before :each do
@game = FactoryGirl.create(:game )
end
context 'PUT#update game updates successfully' do
it 'locates the requested @game' do
# @game = Game.stub(:update).and_return(:game => FactoryGirl.attributes_for(:game))
put :update, :id => @game.id, :game => FactoryGirl.attributes_for(:game)
expect(assigns :game).to eq(@game)
end
it 'changes @games attributes' do
put :update, :id => @game.id, game: FactoryGirl.attributes_for(:game, :title => 'Of Arabia', :description => 'Larry in the Desert', :minimum_players => 2, :maximum_players => 4 )
@game.reload
@game.title.should eq('Of Arabia')
@game.description.should eq('Larry in the Desert')
@game.minimum_players.should eq(2)
@game.maximum_players.should eq(4)
end
it 'redirects to the updated game' do
put :update, id: @game.id, game: FactoryGirl.attributes_for(:game)
expect(response).to redirect_to :game
end
end
context 'PUT#update game does not successfully update due to invalid attributes' do
it 'locates the requested @game' do
put :update, :id => @game.id, game: FactoryGirl.attributes_for(:game)
expect(assigns :game).to eq(@game)
end
it 'does not change @game attributes' do
put :update, :id => @game.id, game: FactoryGirl.attributes_for(:game, :title => nil, :description => "Larry in the Desert", :minimum_players => 2, :maximum_players => 2 )
@game.reload
@game.description.should_not eq("Larry in the Desert")
@game.minimum_players.should_not eq("2")
@game.maximum_players.should_not eq("2")
end
it 're-renders the edit page' do
put :update, :id => @game.id, game: FactoryGirl.attributes_for(:game, :title => nil, :description => "Larry in the Desert", :minimum_players => 2, :maximum_players => 2 )
expect(response).to render_template :edit
end
end
end
describe 'PATCH#update' do
before :each do
@game = FactoryGirl.create(:game )
end
context 'PATCH#update game updates successfully' do
it 'locates the requested @game' do
patch :update, :id => @game.id, :game => FactoryGirl.attributes_for(:game)
expect(assigns :game).to eq(@game)
end
it 'changes @games attributes' do
patch :update, :id => @game.id, game: FactoryGirl.attributes_for(:game, :title => 'Of Arabia', :description => 'Larry in the Desert', :minimum_players => 2, :maximum_players => 4 )
@game.reload
@game.title.should eq('Of Arabia')
@game.description.should eq('Larry in the Desert')
@game.minimum_players.should eq(2)
@game.maximum_players.should eq(4)
end
it 'redirects to the updated game' do
patch :update, id: @game.id, game: FactoryGirl.attributes_for(:game)
expect(response).to redirect_to :game
end
end
context 'PATCH#update game does not successfully update due to invalid attributes' do
it 'locates the requested @game' do
patch :update, :id => @game.id, game: FactoryGirl.attributes_for(:game)
expect(assigns :game).to eq(@game)
end
it 'does not change @game attributes' do
patch :update, :id => @game.id, game: FactoryGirl.attributes_for(:game, :title => nil, :description => "Larry in the Desert", :minimum_players => 2, :maximum_players => 2 )
@game.reload
@game.description.should_not eq("Larry in the Desert")
@game.minimum_players.should_not eq("2")
@game.maximum_players.should_not eq("2")
end
it 're-renders the edit page' do
patch :update, :id => @game.id, game: FactoryGirl.attributes_for(:game, :title => nil, :description => "Larry in the Desert", :minimum_players => 2, :maximum_players => 2 )
expect(response).to render_template :edit
end
end
end
describe 'DELETE#destroy' do
before do
@fake_game = FactoryGirl.create(:game)
end
it 'finds the game by id' do
Game.stub find: @fake_game
delete :destroy, id: @fake_game
expect(Game).to have_received(:find).with(@fake_game.id.to_s)
end
it 'calls #destroy on the game' do
Game.stub find: @fake_game
@fake_game.stub destroy: true
delete :destroy, id: @fake_game
expect(@fake_game).to have_received(:destroy)
end
it "destroys the game" do
expect{ delete :destroy, id: @fake_game }.to change(Game,:count).by(-1)
end
it 'redirects to All Games' do
delete :destroy, id: @fake_game
expect(response).to redirect_to(games_path)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment