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

@@ -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

View File

@@ -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

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
subject { playlist }
let(:playlist) { Playlist.new :name => 'Electro' }
let(:playlist) { FactoryGirl.build(:playlist) }
context 'with valid attributes' do
it { should be_valid }

View File

@@ -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 }