From ac945752504ed86bc9e93462bfa6835a656a0240 Mon Sep 17 00:00:00 2001 From: Matt Moriarity Date: Thu, 16 Aug 2007 19:10:53 -0400 Subject: [PATCH] The code is now completely covered in specs --- .gitignore | 3 +- app/controllers/shows_controller.rb | 12 +++---- spec/controllers/shows_controller_spec.rb | 23 +++++++++++-- spec/controllers/songs_controller_spec.rb | 30 ++++++++++++++++- spec/helpers/application_helper_spec.rb | 56 +++++++++++++++++++++---------- spec/models/song_spec.rb | 22 ++++++++++++ 6 files changed, 117 insertions(+), 29 deletions(-) diff --git a/.gitignore b/.gitignore index b7148f1..97f6515 100644 --- a/.gitignore +++ b/.gitignore @@ -3,4 +3,5 @@ ._* *~ tmp -*.lsz \ No newline at end of file +*.lsz +coverage \ No newline at end of file diff --git a/app/controllers/shows_controller.rb b/app/controllers/shows_controller.rb index 9363269..38c5818 100644 --- a/app/controllers/shows_controller.rb +++ b/app/controllers/shows_controller.rb @@ -12,10 +12,8 @@ class ShowsController < ApplicationController make_resourceful do # These are the only actions we need for now - actions :index, :show, :edit, :destroy + actions :show, :edit, :destroy - # This URL should not be used - response_for(:index) { set_default_redirect home_path } # The slideshow needs to use the S5 layout response_for(:show) { render :layout => false } # We want to avoid the index URL @@ -25,6 +23,9 @@ class ShowsController < ApplicationController end end + # Avoid loading shows if we don't use them + def index; redirect_to home_path; end + # This behaves a little special # # See SongsController#new @@ -38,10 +39,7 @@ class ShowsController < ApplicationController # # This should be called from an Ajax request. def reorder - Show.find(params[:id]).order! params[:songs_list] - # params[:songs_list].each_with_index do |id, idx| - # Usage.update(id, :position => idx + 1) - # end + current_model.find(params[:id]).order! params[:songs_list] render :nothing => true end diff --git a/spec/controllers/shows_controller_spec.rb b/spec/controllers/shows_controller_spec.rb index 038462c..f839309 100644 --- a/spec/controllers/shows_controller_spec.rb +++ b/spec/controllers/shows_controller_spec.rb @@ -1,6 +1,6 @@ require File.dirname(__FILE__) + '/../spec_helper' -describe ShowsController do +describe ShowsController, "logged in" do before do @show = mock_model(Show) @shows_proxy = mock("proxy") @@ -15,8 +15,27 @@ describe ShowsController do end it "should update positions of all songs in show on reorder" do - Show.should_receive(:find).and_return(@show) + @shows_proxy.should_receive(:find).and_return(@show) @show.should_receive(:order!).with(['2', '1']) put 'reorder', :id => '1', :songs_list => ['2', '1'] end + + it "should redirect to home on index" do + get 'index' + response.should redirect_to('/') + end + + it "should redirect to home on destroy" do + @shows_proxy.should_receive(:find).and_return(@show) + @show.should_receive(:destroy).and_return(true) + delete 'destroy', :id => '1' + response.should redirect_to('/') + end + + it "should display a message to the user on destroy" do + @shows_proxy.should_receive(:find).and_return(@show) + @show.should_receive(:destroy).and_return(true) + delete 'destroy', :id => '1' + flash[:notice].should_not be_blank + end end diff --git a/spec/controllers/songs_controller_spec.rb b/spec/controllers/songs_controller_spec.rb index ec9017e..72443ab 100644 --- a/spec/controllers/songs_controller_spec.rb +++ b/spec/controllers/songs_controller_spec.rb @@ -1,6 +1,6 @@ require File.dirname(__FILE__) + '/../spec_helper' -describe SongsController do +describe SongsController, "logged in" do before do @song = mock_model(Song) @songs_proxy = mock("proxy") @@ -13,4 +13,32 @@ describe SongsController do get 'new' @response.should redirect_to("/songs/#{@song.id}/edit") end + + it "should redirect to home on destroy" do + @songs_proxy.should_receive(:find).and_return(@song) + @song.should_receive(:destroy).and_return(true) + delete 'destroy', :id => '1' + response.should redirect_to('/') + end + + it "should display a message to the user on destroy" do + @songs_proxy.should_receive(:find).and_return(@song) + @song.should_receive(:destroy).and_return(true) + delete 'destroy', :id => '1' + flash[:notice].should_not be_blank + end + + it "should redirect to edit page after update" do + @songs_proxy.should_receive(:find).and_return(@song) + @song.should_receive(:update_attributes).and_return(true) + put 'update', :id => '1', :song => {:lyrics => "blah"} + response.should redirect_to("/songs/#{@song.id}/edit") + end + + it "should display a message to the user on update" do + @songs_proxy.should_receive(:find).and_return(@song) + @song.should_receive(:update_attributes).and_return(true) + put 'update', :id => '1', :song => {:lyrics => "blah"} + flash[:notice].should_not be_blank + end end diff --git a/spec/helpers/application_helper_spec.rb b/spec/helpers/application_helper_spec.rb index a4037fc..3fd08fc 100644 --- a/spec/helpers/application_helper_spec.rb +++ b/spec/helpers/application_helper_spec.rb @@ -1,8 +1,16 @@ require File.dirname(__FILE__) + '/../spec_helper' -describe "ApplicationHelper with a current song" do - helper_name :application - +describe ApplicationHelper do + it "should generate an id and class for an in-place editor" do + @song = mock_model(Song) + result = in_place_options(:song, :title) + + result[:id].should include("song_title_#{@song.id}") + result[:class].should == 'in_place_editor_field' + end +end + +describe ApplicationHelper, "with a current song" do it "should give the current song a current CSS class" do @song = mock_model(Song) song_class(@song).should include('current') @@ -14,17 +22,13 @@ describe "ApplicationHelper with a current song" do end end -describe "ApplicationHelper without a current song" do - helper_name :application - +describe ApplicationHelper, "without a current song" do it "should not give a current CSS class if there is no current song" do song_class(mock_model(Song)).should_not include('current') end end -describe "ApplicationHelper with a current show" do - helper_name :application - +describe ApplicationHelper, "with a current show" do it "should give the current show a current CSS class" do @show = mock_model(Show) show_class(@show).should include('current') @@ -36,17 +40,13 @@ describe "ApplicationHelper with a current show" do end end -describe "ApplicationHelper without a current show" do - helper_name :application - +describe ApplicationHelper, "without a current show" do it "should not give a current CSS class if there is no current show" do show_class(mock_model(Show)).should_not include('current') end end -describe "ApplicationHelper logged in" do - helper_name :application - +describe ApplicationHelper, "logged in" do before do @songs = mock("songs_proxy") @shows = mock("shows_proxy") @@ -66,9 +66,7 @@ describe "ApplicationHelper logged in" do end end -describe "ApplicationHelper logged out" do - helper_name :application - +describe ApplicationHelper, "logged out" do before do stub!(:logged_in?).and_return(false) end @@ -80,4 +78,26 @@ describe "ApplicationHelper logged out" do it "should retrieve an empty list of shows" do all_shows.size.should == 0 end +end + +describe ApplicationHelper, "with a flash notice" do + before do + @flash = mock("flash") + end + + it "should display notice box if there is a flash notice" do + @flash.should_receive(:has_key?).with(:notice).and_return(true) + notice_options.should_not have_key(:style) + end +end + +describe ApplicationHelper, "without a flash notice" do + before do + @flash = mock("flash") + end + + it "should not display notice box if there is no flash notice" do + @flash.should_receive(:has_key?).with(:notice).and_return(false) + notice_options.should have_key(:style) + end end \ No newline at end of file diff --git a/spec/models/song_spec.rb b/spec/models/song_spec.rb index f38feaf..33f77e2 100644 --- a/spec/models/song_spec.rb +++ b/spec/models/song_spec.rb @@ -18,3 +18,25 @@ describe Song do end end + +describe Song, "with an artist" do + before(:each) do + @song = Song.new + end + + it "should return the artist" do + @song.artist = "An Artist" + @song.artist.should_not be_nil + end +end + +describe Song, "without an artist" do + before(:each) do + @song = Song.new + end + + it "should return an empty string for the artist" do + @song.artist.should_not be_nil + @song.artist.should == '' + end +end \ No newline at end of file -- 2.11.4.GIT