Use factory_girl_rails for factories

This commit is contained in:
Thibault Jouan 2011-07-25 22:08:45 +00:00
parent 6af96b0f75
commit 47fa969617
8 changed files with 35 additions and 35 deletions

View File

@ -11,4 +11,5 @@ group :development, :test do
gem 'cucumber-rails' gem 'cucumber-rails'
gem 'capybara' gem 'capybara'
gem 'database_cleaner' gem 'database_cleaner'
gem 'factory_girl_rails'
end end

View File

@ -53,6 +53,10 @@ GEM
diff-lcs (1.1.2) diff-lcs (1.1.2)
erubis (2.6.6) erubis (2.6.6)
abstract (>= 1.0.0) 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) ffi (1.0.9)
gherkin (2.4.1) gherkin (2.4.1)
json (>= 1.4.6) json (>= 1.4.6)
@ -124,6 +128,7 @@ DEPENDENCIES
capybara capybara
cucumber-rails cucumber-rails
database_cleaner database_cleaner
factory_girl_rails
haml haml
rails (= 3.0.9) rails (= 3.0.9)
rspec-rails rspec-rails

View File

@ -8,7 +8,7 @@ class PlaylistsController < ApplicationController
end end
def create def create
@playlist = Playlist.new params[:playlist] @playlist = Playlist.new(:name => params[:playlist][:name])
if @playlist.save if @playlist.save
redirect_to :action => 'index' redirect_to :action => 'index'
else else

View File

@ -1,15 +1,9 @@
require 'spec_helper' require 'spec_helper'
describe PlaylistsController do describe PlaylistsController do
def valid_attributes
{
:name => 'Electro'
}
end
describe 'GET index' do describe 'GET index' do
it 'assigns all playlists as @playlists' do it 'assigns all playlists as @playlists' do
playlist = Playlist.create! valid_attributes playlist = Factory.create(:playlist)
get :index get :index
assigns[:playlists].should == [playlist] assigns[:playlists].should == [playlist]
end end
@ -24,7 +18,7 @@ describe PlaylistsController do
describe 'GET edit' do describe 'GET edit' do
it 'assigns the requested playlist as @playlist' 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 get :edit, :id => playlist.id.to_s
assigns[:playlist].should == playlist assigns[:playlist].should == playlist
end end
@ -32,26 +26,23 @@ describe PlaylistsController do
describe 'POST create' do describe 'POST create' do
let(:playlist) { mock_model(Playlist).as_null_object } let(:playlist) { mock_model(Playlist).as_null_object }
before { Playlist.stub(:new).and_return(playlist) }
before do
Playlist.stub(:new).and_return(playlist)
end
it 'creates a new playlist' do it 'creates a new playlist' do
Playlist.should_receive(:new). Playlist.should_receive(:new).
with('name' => 'Electro'). with(Factory.attributes_for(:playlist)).
and_return(playlist) and_return(playlist)
post :create, :playlist => { 'name' => 'Electro' } post :create, :playlist => Factory.attributes_for(:playlist)
end end
it 'saves the playlist' do it 'saves the playlist' do
playlist.should_receive(:save) playlist.should_receive(:save)
post :create post :create, :playlist => {}
end end
context 'when the playlist saves successfully' do context 'when the playlist saves successfully' do
it 'redirects to the playlists index' do it 'redirects to the playlists index' do
post :create post :create, :playlist => {}
response.should redirect_to(:action => 'index') response.should redirect_to(:action => 'index')
end end
end end
@ -62,12 +53,12 @@ describe PlaylistsController do
end end
it 'assigns the playlist as @playlist' do it 'assigns the playlist as @playlist' do
post :create post :create, :playlist => {}
assigns[:playlist].should eq(playlist) assigns[:playlist].should eq(playlist)
end end
it 'renders the new template' do it 'renders the new template' do
post :create post :create, :playlist => {}
response.should render_template('new') response.should render_template('new')
end end
end end
@ -75,22 +66,22 @@ describe PlaylistsController do
describe 'PUT update' do describe 'PUT update' do
it 'updates the playlist' do it 'updates the playlist' do
playlist = Playlist.create! valid_attributes playlist = Factory.create(:playlist)
Playlist.any_instance.should_receive(:update_attributes). Playlist.any_instance.should_receive(:update_attributes).
with({'name' => 'Rock'}) with({'name' => 'Rock'})
put :update, :id => playlist.id.to_s, :playlist => {:name => 'Rock'} put :update, :id => playlist.id.to_s, :playlist => {:name => 'Rock'}
end end
it 'assigns the requested playlist as @playlist' do 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'} put :update, :id => playlist.id.to_s, :playlist => {:name => 'Rock'}
assigns[:playlist].should == playlist assigns[:playlist].should == playlist
end end
context 'when the playlist updates successfully' do context 'when the playlist updates successfully' do
it 'redirects to the playlists index' do it 'redirects to the playlists index' do
playlist = Playlist.create! valid_attributes playlist = Factory.create(:playlist)
put :update, :id => playlist.id.to_s, :playlist => valid_attributes put :update, :id => playlist.id.to_s, :playlist => Factory.attributes_for(:playlist)
response.should redirect_to(:action => 'index') response.should redirect_to(:action => 'index')
end end
end end

View File

@ -1,15 +1,9 @@
require 'spec_helper' require 'spec_helper'
describe TracksController do describe TracksController do
def valid_attributes
{
:name => 'Mega song'
}
end
describe 'GET show' do describe 'GET show' do
it 'assigns the requested track as @track' 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 get :show, :id => track.id.to_s
assigns[:track].should == track assigns[:track].should == track
end end
@ -28,9 +22,9 @@ describe TracksController do
it 'creates a new track' do it 'creates a new track' do
Track.should_receive(:new). Track.should_receive(:new).
with(:name => 'Mega song'). with(Factory.attributes_for(:track)).
and_return(track) and_return(track)
post :create, :track => valid_attributes post :create, :track => Factory.attributes_for(:track)
end end
it 'saves the track' do it 'saves the track' do
@ -46,7 +40,7 @@ describe TracksController do
context 'when the track saves successfully' do context 'when the track saves successfully' do
it 'redirects to the track page' 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) response.should redirect_to(track)
end end
end end

9
spec/factories.rb Normal file
View File

@ -0,0 +1,9 @@
FactoryGirl.define do
factory :playlist do
name 'Electro'
end
factory :track do
name 'Mega song'
end
end

View File

@ -2,7 +2,7 @@ require 'spec_helper'
describe Playlist do describe Playlist do
subject { playlist } subject { playlist }
let(:playlist) { Playlist.new :name => 'Electro' } let(:playlist) { FactoryGirl.build(:playlist) }
context 'with valid attributes' do context 'with valid attributes' do
it { should be_valid } it { should be_valid }

View File

@ -2,7 +2,7 @@ require 'spec_helper'
describe Track do describe Track do
subject { track } subject { track }
let(:track) { Track.new :name => 'Mega song' } let(:track) { FactoryGirl.build(:track) }
context 'with valid attributes' do context 'with valid attributes' do
it { should be_valid } it { should be_valid }