From 47fa969617b5af244fe27c77645dd3e8aa5892c9 Mon Sep 17 00:00:00 2001 From: Thibault Jouan Date: Mon, 25 Jul 2011 22:08:45 +0000 Subject: [PATCH] Use factory_girl_rails for factories --- Gemfile | 1 + Gemfile.lock | 5 +++ app/controllers/playlists_controller.rb | 2 +- spec/controllers/playlists_controller_spec.rb | 35 +++++++------------ spec/controllers/tracks_controller_spec.rb | 14 +++----- spec/factories.rb | 9 +++++ spec/models/playlist_spec.rb | 2 +- spec/models/track_spec.rb | 2 +- 8 files changed, 35 insertions(+), 35 deletions(-) create mode 100644 spec/factories.rb diff --git a/Gemfile b/Gemfile index 76a3e87..b28f072 100644 --- a/Gemfile +++ b/Gemfile @@ -11,4 +11,5 @@ group :development, :test do gem 'cucumber-rails' gem 'capybara' gem 'database_cleaner' + gem 'factory_girl_rails' end diff --git a/Gemfile.lock b/Gemfile.lock index 8a4c265..001a0d3 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -53,6 +53,10 @@ GEM diff-lcs (1.1.2) erubis (2.6.6) abstract (>= 1.0.0) + factory_girl (2.0.1) + factory_girl_rails (1.1.0) + factory_girl (~> 2.0.0) + railties (>= 3.0.0) ffi (1.0.9) gherkin (2.4.1) json (>= 1.4.6) @@ -124,6 +128,7 @@ DEPENDENCIES capybara cucumber-rails database_cleaner + factory_girl_rails haml rails (= 3.0.9) rspec-rails diff --git a/app/controllers/playlists_controller.rb b/app/controllers/playlists_controller.rb index 03a56a3..830ea84 100644 --- a/app/controllers/playlists_controller.rb +++ b/app/controllers/playlists_controller.rb @@ -8,7 +8,7 @@ class PlaylistsController < ApplicationController end def create - @playlist = Playlist.new params[:playlist] + @playlist = Playlist.new(:name => params[:playlist][:name]) if @playlist.save redirect_to :action => 'index' else diff --git a/spec/controllers/playlists_controller_spec.rb b/spec/controllers/playlists_controller_spec.rb index 0149f42..1ef894c 100644 --- a/spec/controllers/playlists_controller_spec.rb +++ b/spec/controllers/playlists_controller_spec.rb @@ -1,15 +1,9 @@ require 'spec_helper' describe PlaylistsController do - def valid_attributes - { - :name => 'Electro' - } - end - describe 'GET index' do it 'assigns all playlists as @playlists' do - playlist = Playlist.create! valid_attributes + playlist = Factory.create(:playlist) get :index assigns[:playlists].should == [playlist] end @@ -24,7 +18,7 @@ describe PlaylistsController do describe 'GET edit' do it 'assigns the requested playlist as @playlist' do - playlist = Playlist.create! valid_attributes + playlist = Factory.create(:playlist) get :edit, :id => playlist.id.to_s assigns[:playlist].should == playlist end @@ -32,26 +26,23 @@ describe PlaylistsController do describe 'POST create' do let(:playlist) { mock_model(Playlist).as_null_object } - - before do - Playlist.stub(:new).and_return(playlist) - end + before { Playlist.stub(:new).and_return(playlist) } it 'creates a new playlist' do Playlist.should_receive(:new). - with('name' => 'Electro'). + with(Factory.attributes_for(:playlist)). and_return(playlist) - post :create, :playlist => { 'name' => 'Electro' } + post :create, :playlist => Factory.attributes_for(:playlist) end it 'saves the playlist' do playlist.should_receive(:save) - post :create + post :create, :playlist => {} end context 'when the playlist saves successfully' do it 'redirects to the playlists index' do - post :create + post :create, :playlist => {} response.should redirect_to(:action => 'index') end end @@ -62,12 +53,12 @@ describe PlaylistsController do end it 'assigns the playlist as @playlist' do - post :create + post :create, :playlist => {} assigns[:playlist].should eq(playlist) end it 'renders the new template' do - post :create + post :create, :playlist => {} response.should render_template('new') end end @@ -75,22 +66,22 @@ describe PlaylistsController do describe 'PUT update' do it 'updates the playlist' do - playlist = Playlist.create! valid_attributes + playlist = Factory.create(:playlist) Playlist.any_instance.should_receive(:update_attributes). with({'name' => 'Rock'}) put :update, :id => playlist.id.to_s, :playlist => {:name => 'Rock'} end it 'assigns the requested playlist as @playlist' do - playlist = Playlist.create! valid_attributes + playlist = Factory.create(:playlist) put :update, :id => playlist.id.to_s, :playlist => {:name => 'Rock'} assigns[:playlist].should == playlist end context 'when the playlist updates successfully' do it 'redirects to the playlists index' do - playlist = Playlist.create! valid_attributes - put :update, :id => playlist.id.to_s, :playlist => valid_attributes + playlist = Factory.create(:playlist) + put :update, :id => playlist.id.to_s, :playlist => Factory.attributes_for(:playlist) response.should redirect_to(:action => 'index') end end diff --git a/spec/controllers/tracks_controller_spec.rb b/spec/controllers/tracks_controller_spec.rb index 45a9671..e4311cc 100644 --- a/spec/controllers/tracks_controller_spec.rb +++ b/spec/controllers/tracks_controller_spec.rb @@ -1,15 +1,9 @@ require 'spec_helper' describe TracksController do - def valid_attributes - { - :name => 'Mega song' - } - end - describe 'GET show' do it 'assigns the requested track as @track' do - track = Track.create! valid_attributes + track = Factory.create(:track) get :show, :id => track.id.to_s assigns[:track].should == track end @@ -28,9 +22,9 @@ describe TracksController do it 'creates a new track' do Track.should_receive(:new). - with(:name => 'Mega song'). + with(Factory.attributes_for(:track)). and_return(track) - post :create, :track => valid_attributes + post :create, :track => Factory.attributes_for(:track) end it 'saves the track' do @@ -46,7 +40,7 @@ describe TracksController do context 'when the track saves successfully' do it 'redirects to the track page' do - post :create, :track => valid_attributes + post :create, :track => Factory.attributes_for(:track) response.should redirect_to(track) end end diff --git a/spec/factories.rb b/spec/factories.rb new file mode 100644 index 0000000..f714180 --- /dev/null +++ b/spec/factories.rb @@ -0,0 +1,9 @@ +FactoryGirl.define do + factory :playlist do + name 'Electro' + end + + factory :track do + name 'Mega song' + end +end diff --git a/spec/models/playlist_spec.rb b/spec/models/playlist_spec.rb index 1f5d62a..8978e77 100644 --- a/spec/models/playlist_spec.rb +++ b/spec/models/playlist_spec.rb @@ -2,7 +2,7 @@ require 'spec_helper' describe Playlist do subject { playlist } - let(:playlist) { Playlist.new :name => 'Electro' } + let(:playlist) { FactoryGirl.build(:playlist) } context 'with valid attributes' do it { should be_valid } diff --git a/spec/models/track_spec.rb b/spec/models/track_spec.rb index 6f66918..9ce782b 100644 --- a/spec/models/track_spec.rb +++ b/spec/models/track_spec.rb @@ -2,7 +2,7 @@ require 'spec_helper' describe Track do subject { track } - let(:track) { Track.new :name => 'Mega song' } + let(:track) { FactoryGirl.build(:track) } context 'with valid attributes' do it { should be_valid }