Commit 109a3e61 authored by Vaz Allen's avatar Vaz Allen

Acceptance tests for albums actions

parent 67210264
...@@ -21,4 +21,5 @@ group :test do ...@@ -21,4 +21,5 @@ group :test do
gem 'rspec' gem 'rspec'
gem 'capybara' gem 'capybara'
gem 'database_cleaner' gem 'database_cleaner'
gem 'faker'
end end
...@@ -29,6 +29,8 @@ GEM ...@@ -29,6 +29,8 @@ GEM
coderay (1.1.0) coderay (1.1.0)
database_cleaner (1.3.0) database_cleaner (1.3.0)
diff-lcs (1.2.5) diff-lcs (1.2.5)
faker (1.6.3)
i18n (~> 0.5)
i18n (0.7.0) i18n (0.7.0)
json (1.8.3) json (1.8.3)
method_source (0.8.2) method_source (0.8.2)
...@@ -109,6 +111,7 @@ DEPENDENCIES ...@@ -109,6 +111,7 @@ DEPENDENCIES
activesupport activesupport
capybara capybara
database_cleaner database_cleaner
faker
pry pry
puma puma
rake rake
......
...@@ -12,3 +12,17 @@ get '/albums' do ...@@ -12,3 +12,17 @@ get '/albums' do
@albums = Album.all @albums = Album.all
erb :'albums/index' erb :'albums/index'
end 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,4 +2,6 @@ class Album < ActiveRecord::Base ...@@ -2,4 +2,6 @@ class Album < ActiveRecord::Base
has_many :songs has_many :songs
validates :title, presence: true
end end
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
<h1> <h1>
Albums Albums
</h1> </h1>
<ul> <ul class="albums">
<% @albums.each do |album| %> <% @albums.each do |album| %>
<li> <%= album.title %> </li> <li> <%= album.title %> </li>
<% end %> <% end %>
......
<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>
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
...@@ -5,6 +5,7 @@ require_relative '../config/environment' ...@@ -5,6 +5,7 @@ require_relative '../config/environment'
require 'rspec' require 'rspec'
require 'capybara/rspec' require 'capybara/rspec'
require 'database_cleaner' require 'database_cleaner'
require 'faker'
Capybara.app = Sinatra::Application Capybara.app = Sinatra::Application
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment