I followed The Ruby on Rails Tutorial Book by Michael Hartl, modified it, added Devise for user authentication and ran into an issue with the uniqueness tests of the email.
# spec/models/user_spec.rb
require 'spec_helper'
describe User do
before do
@user = User.new(username: 'ExampleUser',
email: 'user@example.com',
password: 'passworD123')
@user.save
end
subject { @user }
it { should respond_to(:email) }
it { should respond_to(:name) }
it { should respond_to(:username) }
it { should respond_to(:password) }
it { should be_valid }
describe 'username is already taken' do
before do
user_with_same_username = @user.dup
user_with_same_username.username = @user.username.upcase
user_with_same_username.email = 'a@b.c'
user_with_same_username.save
end
it { should_not be_valid }
end
describe 'email address is already taken' do
before do
user_with_same_email = @user.dup
user_with_same_email.username = 'differentUsername'
user_with_same_email.email = @user.email.upcase
user_with_same_email.save
end
it { should_not be_valid }
end
.
.
Failures:
1) User email address is already taken
Failure/Error: it { should_not be_valid }
expected valid? to return false, got true
# ./spec/models/user_spec.rb:104:in `block (3 levels) in <top (required)>'
The uniqueness of email is set although it is not needed because Devise does it already. I changed user_with_same_email.save to user_with_same_email.save! and then I got a validation error for the email address because it’s already taken:
1) User email address is already taken
Failure/Error: user_with_same_email.save!
ActiveRecord::RecordInvalid:
Validation failed: Email has already been taken
# ./spec/models/user_spec.rb:101:in `block (3 levels) in <top (required)>'
The return value of user_with_same_email.save is false and it { should_not be_valid } checks this case, but why is the test still failing?
Concerning your actual issue, the reason is simple: you’re not testing upon the proper
subjectOther guidelines:
Do not save an object if you know it’s invalid, you’re just wasting time.
Same for your first specs, do you really need a persisted object? I guess not..
Last but not least: consider using factories, it’s reusable and will let you have more consistent tests.
Here is what your specs could look like: