Bundle shoulda and use ActiveModel matchers in specs

This commit is contained in:
Thibault Jouan 2011-09-21 00:07:19 +00:00
parent 72f800397d
commit 6af1540640
7 changed files with 27 additions and 84 deletions

View File

@ -18,4 +18,5 @@ group :development, :test do
gem 'guard-rspec' gem 'guard-rspec'
gem 'guard-spork' gem 'guard-spork'
gem 'rb-inotify' gem 'rb-inotify'
gem 'shoulda-matchers'
end end

View File

@ -117,6 +117,7 @@ GEM
ffi (>= 1.0.7) ffi (>= 1.0.7)
json_pure json_pure
rubyzip rubyzip
shoulda-matchers (1.0.0.beta3)
spork (0.9.0.rc9) spork (0.9.0.rc9)
sprockets (2.0.0) sprockets (2.0.0)
hike (~> 1.2) hike (~> 1.2)
@ -147,4 +148,5 @@ DEPENDENCIES
rails (= 3.1.0) rails (= 3.1.0)
rb-inotify rb-inotify
rspec-rails rspec-rails
shoulda-matchers
spork (~> 0.9.0.rc) spork (~> 0.9.0.rc)

View File

@ -1,6 +1,6 @@
class Playlist < ActiveRecord::Base class Playlist < ActiveRecord::Base
belongs_to :user belongs_to :user
validates_presence_of :user_id validates_presence_of :user
validates_presence_of :name validates_presence_of :name
end end

View File

@ -1,34 +1,11 @@
require 'spec_helper' require 'spec_helper'
describe Playlist do describe Playlist do
subject { playlist } subject { playlist }
let(:playlist) { Factory.build(:playlist) } let(:playlist) { Factory.build(:playlist) }
context 'with valid attributes' do it { should be_valid }
it { should be_valid } it { should belong_to :user }
end it { should validate_presence_of :user }
it { should validate_presence_of :name }
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
end end

View File

@ -4,19 +4,10 @@ describe Sound do
subject { sound } subject { sound }
let(:sound) { Factory.build(:sound) } let(:sound) { Factory.build(:sound) }
context 'with valid attributes' do it { should be_valid }
it { should be_valid } it { should belong_to :track }
end it { should validate_presence_of :sha256 }
it { should validate_presence_of :mime_type }
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
describe '#path' do describe '#path' do
it 'returns the sound file path based on the SHA256 digest' do it 'returns the sound file path based on the SHA256 digest' do

View File

@ -4,14 +4,9 @@ describe Track do
subject { track } subject { track }
let(:track) { Factory.build(:track) } let(:track) { Factory.build(:track) }
context 'with valid attributes' do it { should be_valid }
it { should be_valid } it { should have_many :sounds }
end it { should validate_presence_of :name }
context 'when name empty' do
before { track.name = '' }
it { should_not be_valid }
end
describe '#file=' do describe '#file=' do
it 'builds a new related sound with the file' do it 'builds a new related sound with the file' do

View File

@ -1,27 +1,20 @@
require 'spec_helper' require 'spec_helper'
describe User do describe User do
subject { user } subject { user }
let(:user) { Factory.build(:user) } let(:user) { Factory.build(:user) }
context 'with valid attributes' do it { should be_valid }
it { should be_valid } it { should validate_presence_of :email }
end it { should validate_presence_of :password }
it { should validate_presence_of :password_hash }
context 'when email empty' do context 'when a user with the same email address already exists' do
before do it 'should not be valid' do
user.email = '' user = Factory.create(:user, :email => 'unique@example.net')
new_user = Factory.build(:user, :email => user.email)
new_user.should_not be_valid
end end
it { should_not be_valid }
end
context 'when password empty' do
before do
user.password = ''
end
it { should_not be_valid }
end end
context 'when password_confirmation does not match password' do context 'when password_confirmation does not match password' do
@ -32,22 +25,6 @@ describe User do
it { should_not be_valid } it { should_not be_valid }
end 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 describe '#password=' do
it 'stores a bcrypt hash of the password in password_hash' do it 'stores a bcrypt hash of the password in password_hash' do
BCrypt::Password.new(user.password_hash).should == user.password BCrypt::Password.new(user.password_hash).should == user.password