From 6af1540640b42f0336c49f67ebab05776ec99294 Mon Sep 17 00:00:00 2001 From: Thibault Jouan Date: Wed, 21 Sep 2011 00:07:19 +0000 Subject: [PATCH] Bundle shoulda and use ActiveModel matchers in specs --- Gemfile | 1 + Gemfile.lock | 2 ++ app/models/playlist.rb | 2 +- spec/models/playlist_spec.rb | 33 ++++---------------------- spec/models/sound_spec.rb | 17 ++++---------- spec/models/track_spec.rb | 11 +++------ spec/models/user_spec.rb | 45 +++++++++--------------------------- 7 files changed, 27 insertions(+), 84 deletions(-) diff --git a/Gemfile b/Gemfile index 733f1ea..5023da3 100644 --- a/Gemfile +++ b/Gemfile @@ -18,4 +18,5 @@ group :development, :test do gem 'guard-rspec' gem 'guard-spork' gem 'rb-inotify' + gem 'shoulda-matchers' end diff --git a/Gemfile.lock b/Gemfile.lock index e69391b..a91e30a 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -117,6 +117,7 @@ GEM ffi (>= 1.0.7) json_pure rubyzip + shoulda-matchers (1.0.0.beta3) spork (0.9.0.rc9) sprockets (2.0.0) hike (~> 1.2) @@ -147,4 +148,5 @@ DEPENDENCIES rails (= 3.1.0) rb-inotify rspec-rails + shoulda-matchers spork (~> 0.9.0.rc) diff --git a/app/models/playlist.rb b/app/models/playlist.rb index fe81427..404af23 100644 --- a/app/models/playlist.rb +++ b/app/models/playlist.rb @@ -1,6 +1,6 @@ class Playlist < ActiveRecord::Base belongs_to :user - validates_presence_of :user_id + validates_presence_of :user validates_presence_of :name end diff --git a/spec/models/playlist_spec.rb b/spec/models/playlist_spec.rb index cb7a7ec..81987d6 100644 --- a/spec/models/playlist_spec.rb +++ b/spec/models/playlist_spec.rb @@ -1,34 +1,11 @@ require 'spec_helper' describe Playlist do - subject { playlist } + subject { playlist } let(:playlist) { Factory.build(:playlist) } - 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 - - context 'without user_id' do - before do - playlist.user = nil - end - - it { should_not be_valid } - end - - describe '#user' do - it 'returns the user who created the playlist' do - user = Factory.create(:user) - playlist = user.playlists.build(Factory.attributes_for(:playlist)) - playlist.user.should == user - end - end + it { should be_valid } + it { should belong_to :user } + it { should validate_presence_of :user } + it { should validate_presence_of :name } end diff --git a/spec/models/sound_spec.rb b/spec/models/sound_spec.rb index cba1ede..1e5571b 100644 --- a/spec/models/sound_spec.rb +++ b/spec/models/sound_spec.rb @@ -4,19 +4,10 @@ describe Sound do subject { sound } let(:sound) { Factory.build(:sound) } - context 'with valid attributes' do - it { should be_valid } - end - - context 'when sha256 empty' do - before { sound.sha256 = '' } - it { should_not be_valid } - end - - context 'when mime_type empty' do - before { sound.mime_type = '' } - it { should_not be_valid } - end + it { should be_valid } + it { should belong_to :track } + it { should validate_presence_of :sha256 } + it { should validate_presence_of :mime_type } describe '#path' do it 'returns the sound file path based on the SHA256 digest' do diff --git a/spec/models/track_spec.rb b/spec/models/track_spec.rb index cfcb169..bccc31c 100644 --- a/spec/models/track_spec.rb +++ b/spec/models/track_spec.rb @@ -4,14 +4,9 @@ describe Track do subject { track } let(:track) { Factory.build(:track) } - context 'with valid attributes' do - it { should be_valid } - end - - context 'when name empty' do - before { track.name = '' } - it { should_not be_valid } - end + it { should be_valid } + it { should have_many :sounds } + it { should validate_presence_of :name } describe '#file=' do it 'builds a new related sound with the file' do diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index 1d290a6..37fcb64 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -1,27 +1,20 @@ require 'spec_helper' describe User do - subject { user } - let(:user) { Factory.build(:user) } + subject { user } + let(:user) { Factory.build(:user) } - context 'with valid attributes' do - it { should be_valid } - end + it { should be_valid } + it { should validate_presence_of :email } + it { should validate_presence_of :password } + it { should validate_presence_of :password_hash } - context 'when email empty' do - before do - user.email = '' + context 'when a user with the same email address already exists' do + it 'should not be valid' do + user = Factory.create(:user, :email => 'unique@example.net') + new_user = Factory.build(:user, :email => user.email) + new_user.should_not be_valid end - - it { should_not be_valid } - end - - context 'when password empty' do - before do - user.password = '' - end - - it { should_not be_valid } end context 'when password_confirmation does not match password' do @@ -32,22 +25,6 @@ describe User do it { should_not be_valid } end - context 'when password_hash empty' do - before do - user.password_hash = '' - end - - it { should_not be_valid } - end - - context 'when a user with the same email address already exists' do - it 'should not be valid' do - user = Factory.create(:user, :email => 'unique@example.net') - new_user = Factory.build(:user, :email => user.email) - new_user.should_not be_valid - end - end - describe '#password=' do it 'stores a bcrypt hash of the password in password_hash' do BCrypt::Password.new(user.password_hash).should == user.password