Add basic playlists management

* Index of playlists
* Create a new playlist
* Basic playlist model with mandatory name
This commit is contained in:
Thibault Jouan
2011-07-09 09:23:32 +00:00
parent 74fb4ca86f
commit 33f979b573
13 changed files with 205 additions and 57 deletions

View File

@@ -0,0 +1,67 @@
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
get :index
assigns[:playlists].should == [playlist]
end
end
describe "GET new" do
it 'assigns a new playlist as @playlist' do
get :new
assigns[:playlist].should be_a_new(Playlist)
end
end
describe "POST create" do
let(:playlist) { mock_model(Playlist).as_null_object }
before do
Playlist.stub(:new).and_return(playlist)
end
it 'creates a new playlist' do
Playlist.should_receive(:new).
with('name' => 'Electro').
and_return(playlist)
post :create, :playlist => { 'name' => 'Electro' }
end
it 'saves the playlist' do
playlist.should_receive(:save)
post :create
end
context 'when the playlist saves successfully' do
it 'redirects to the playlists index' do
post :create
response.should redirect_to(:action => 'index')
end
end
context 'when the playlist fails to save' do
before do
playlist.stub(:save).and_return(false)
end
it 'assigns the playlist as @playlist' do
post :create
assigns[:playlist].should eq(playlist)
end
it 'renders the new template' do
post :create
response.should render_template('new')
end
end
end
end

View File

@@ -0,0 +1,18 @@
require 'spec_helper'
describe Playlist do
subject { playlist }
let(:playlist) { Playlist.new :name => 'Electro' }
context "with valid attributes" do
it { should be_valid }
end
context "when name empty" do
before do
playlist.name = ''
end
it { should_not be_valid }
end
end

View File

@@ -0,0 +1,22 @@
require 'spec_helper'
describe 'playlists/index.html.haml' do
before do
assign :playlists, [
double('Playlist', :name => 'Foo'),
double('Playlist', :name => 'Bar')
]
end
it 'displays a list of playlists' do
render
rendered.should have_selector('ul>li', :count => 2)
rendered.should have_selector('ul>li', :text => 'Foo')
rendered.should have_selector('ul>li+li', :text => 'Bar')
end
it 'displays a link to create a new playlist' do
render
rendered.should have_selector('a', :text => 'Create playlist')
end
end

View File

@@ -0,0 +1,26 @@
require 'spec_helper'
describe 'playlists/new.html.haml' do
let(:playlist) do
mock_model('Playlist').as_new_record.as_null_object
end
before do
assign :playlist, playlist
end
it 'renders a form to create a playlist' do
render
rendered.should have_selector("form[method=post][action='#{playlists_path}']")
rendered.should have_selector('input[type=submit]')
end
it 'renders a text field with a label for the playlists name' do
playlist.stub(:name => 'Electro')
render
rendered.should \
have_selector("input[type=text][name='playlist[name]'][value=Electro]")
rendered.should \
have_selector("label[for=playlist_name]", :text => 'Name')
end
end