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
Within your test, just try to create two users with same e-mail address. Second one should not have valid email. Something like this:
Note that I’m using
factory_girlandvalid_attributegems in the example above.I’m also assuming that you have
validates :email, uniqueness: truein your model.