Reputation: 343
How can I write an Rspec test for the following? Using TDD, I want to be able to write a test to that requires the following code in order to pass.
class AddEmailUniquenessIndex < ActiveRecord::Migration
def up
add_index :users, :email, :unqiue => true
end
def down
remove_index :users, :email
end
end
Upvotes: 1
Views: 658
Reputation: 1580
Within your test, just try to create two users with same e-mail address. Second one should not have valid email. Something like this:
it "prevents duplicates" do
user1 = create(:user, email: '[email protected]')
user2 = build(:user, email: '[email protected]')
user1.should be_valid
user2.should_not have_valid(:email)
end
Note that I'm using factory_girl
and valid_attribute
gems in the example above.
I'm also assuming that you have validates :email, uniqueness: true
in your model.
Upvotes: 3