My trouble arose in Chapter 11 of the Ruby on Rails Tutorial here.
I was seeing this rspec error:
Failure/Error: :user => Factory(:user, :email => Factory.next(:email)))
ActiveRecord::RecordInvalid:
Validation failed: Email has already been taken
first in user_spec.rb
then in micropost_spec.rb
. It was pretty puzzling. I thought the factory statements were generating a user in a fresh testing db each time autotest ran rspec. I checked out source files from the master branch with git and tried again, but saw the same error. I therefore suspected it related to the db contents somehow and not the code.
So, I did the following:
restarted "rails s"
restarted autotest
rake db:reset
rake db:migrate
rake db:test:prepare
rake db:populate
... and it all went green. The rspec tests passed.
There may be a more “to the point” solution, but I was thrilled this worked. Hope it helps someone else. I am left to conclude that my testing/development somehow added something to the db that was unexpected. I suppose the above steps are a good way to make yourself a fresh db near the end of chapter 11.
Was there a more direct way to solve this? Does the error indicate some other issue that I addressed without realizing it? I am left thinking that running rspec does not guarantee a fresh testing db each time. Is that a wrong assumption?