Skip to content

Commit

Permalink
Acceptance tests for albums actions
Browse files Browse the repository at this point in the history
  • Loading branch information
vaz committed May 19, 2016
1 parent 6721026 commit 109a3e6
Show file tree
Hide file tree
Showing 8 changed files with 122 additions and 1 deletion.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,5 @@ group :test do
gem 'rspec'
gem 'capybara'
gem 'database_cleaner'
gem 'faker'
end
3 changes: 3 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ GEM
coderay (1.1.0)
database_cleaner (1.3.0)
diff-lcs (1.2.5)
faker (1.6.3)
i18n (~> 0.5)
i18n (0.7.0)
json (1.8.3)
method_source (0.8.2)
Expand Down Expand Up @@ -109,6 +111,7 @@ DEPENDENCIES
activesupport
capybara
database_cleaner
faker
pry
puma
rake
Expand Down
14 changes: 14 additions & 0 deletions app/actions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,17 @@
@albums = Album.all
erb :'albums/index'
end

get '/albums/new' do
@album = Album.new
erb :'albums/new'
end

post '/albums' do
@album = Album.new(params.slice('title', 'record_label', 'release_date'))
if @album.save
redirect to('/albums')
else
erb :'albums/new'
end
end
2 changes: 2 additions & 0 deletions app/models/album.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@ class Album < ActiveRecord::Base

has_many :songs

validates :title, presence: true

end
2 changes: 1 addition & 1 deletion app/views/albums/index.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<h1>
Albums
</h1>
<ul>
<ul class="albums">
<% @albums.each do |album| %>
<li> <%= album.title %> </li>
<% end %>
Expand Down
18 changes: 18 additions & 0 deletions app/views/albums/new.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<h1>
New Album
</h1>

<% if @album.errors.any? %>
<ul class="errors">
<% @album.errors.full_messages.each do |error| %>
<li><%= error %></li>
<% end %>
</ul>
<% end %>

<form action="/albums" method="post">
<input name="title" value="<%= @album.title %>">
<input name="record_label" value="<%= @album.record_label %>">
<input type="date" name="release_date" value="<%= @album.release_date %>">
<input type="submit" value="Create">
</form>
82 changes: 82 additions & 0 deletions spec/acceptance/album_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
require_relative '../spec_helper'


describe 'Albums scenarios' do
# the page seen by the user is the subject of the tests.
# this makes it the implicit receiver in the one-liners: it { ... }
subject { page }

context 'viewing the index' do

context 'when there are no albums' do
# clean database ensures no albums already

before { visit '/albums' }

it { should_not have_selector('ul.albums li') }
end

context 'when there are albums' do
before do
3.times { Album.create!(title: Faker::Book.title) } # book? close enough
visit '/albums'
end

it { should have_selector('ul.albums li', count: 3) }
end

end

context 'creating an album' do
before { visit '/albums/new' }

context 'when I view the form' do
it { should have_selector('form') } # basic test shows the action works
end

context 'when I submit the form with valid data' do
let(:album_title) { Faker::Book.title }

before do
fill_in :title, with: album_title
fill_in :record_label, with: Faker::Name.name
fill_in :release_date, with: 1.year.ago
click_button 'Create'
end

it 'should redirect to the index' do
expect(page).to have_selector('h1', text: 'Albums')
expect(page).to have_selector('ul.albums')
end

it 'should show the album in the list' do
expect(page).to have_selector('ul.albums li', text: album_title)
end
end

context 'when I submit the form without a title' do
let(:album_record_label) { Faker::Name.name }

before do
fill_in :record_label, with: album_record_label
click_button 'Create'
end

it 'should render the form again' do
expect(page).to have_content('New Album')
expect(page).to have_selector('form')
end

it { should have_selector('.errors', text: "Title can't be blank") }

it 'should preserve the submitted form data' do
expect(page).to have_field('record_label', with: album_record_label)
end

# to be clear, because page is the subject, this is the same as above,
# but with a less descriptive message:
# it { should have_field('record_label', with: album_record_label) }
end

end
end
1 change: 1 addition & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
require 'rspec'
require 'capybara/rspec'
require 'database_cleaner'
require 'faker'

Capybara.app = Sinatra::Application

Expand Down

0 comments on commit 109a3e6

Please sign in to comment.