Skip to content

Instantly share code, notes, and snippets.

@terenceponce
Created April 3, 2012 08:36
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save terenceponce/2290470 to your computer and use it in GitHub Desktop.
Save terenceponce/2290470 to your computer and use it in GitHub Desktop.
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
Copy link
Author

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