-
Notifications
You must be signed in to change notification settings - Fork 258
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
122 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,4 +21,5 @@ group :test do | |
gem 'rspec' | ||
gem 'capybara' | ||
gem 'database_cleaner' | ||
gem 'faker' | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,4 +2,6 @@ class Album < ActiveRecord::Base | |
|
||
has_many :songs | ||
|
||
validates :title, presence: true | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters