I'm currently developing a RoR 4 application.
In my application, I'm trying to implement Users tests similar to Mickael Hartl's tutorial. I'd like to understand a few things about the eMail uniqueness test :
1 - Uniqueness is tested when actually writing to the database, right ?
When writing my test the same way as in the tutorial, it is not efficient. The @user = User.new(...) actually instantiates the user in memory only. The following test:
describe "when email address is already used" do
before do
user_with_same_email = @user.dup
user_with_same_email.save
end
it { should_not be_valid }
end
works only, in my case, when the @user = user.new(...) instruction is followed by a user.save instruction. Am I right ?
Additional note: when email uniqueness is implemented in the user model, it always fails !?! Can you tell me what is wrong with this test ?
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
validates :email, presence: true, uniqueness: true, length: { maximum: 100 }, format: { with: VALID_EMAIL_REGEX }
2 - Does RSPEC really write to the TEST database ?
After running the above test (user.new(...) - user.save - user.dup - user.save), I check the 'modified date' of the TEST SQLite3 database file. It has not been touched when running the test. The spec_hepler.rb files starts with:
ENV["RAILS_ENV"] ||= 'test'
Did I miss something ?
Thank you for your help,
Best regards,
Fred