Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Unit testing validatable module of Devise
Failures:
1) User when email address is already taken
Failure/Error: it { should_not be_valid }
expected valid? to return false, got true
# ./spec/models/user_spec.rb:73:in `block (3 levels) in <top (required)>'
class User < ActiveRecord::Base
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
attr_accessible :email, :password, :password_confirmation, :remember_me
end
require 'spec_helper'
describe User do
before do
@user = Fabricate(:user)
end
subject { @user }
describe 'when email address is already taken' do
before do
user_with_same_email = @user.dup
user_with_same_email.email = @user.email.upcase
user_with_same_email.save
end
it { should_not be_valid }
end
end
@terenceponce

This comment has been minimized.

Copy link
Owner Author

terenceponce commented Apr 3, 2012

As sevenseacat on IRC pointed out, the test was still asserting @user instead of user_with_same_email. That's why it was returning true. Knowing that, I adjusted the test to be like this:

describe 'when email address is already taken' do
  before do
    @user_with_same_email = @user.dup
    @user_with_same_email.email = @user.email
    @user_with_same_email.save
  end

  subject { @user_with_same_email }

  it { should_not be_valid }
end

This solution got the test to pass.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.