ruby on rails tutorial TDD "Email has already been taken"
I have been going through Michael Hartl's tutorial http://railstutorial.org/ and for the most part it has been a huge help in getting started with Rails. The book is very focused on TDD, which is great because I wanted to learn TDD, but the problem is 90% of my tests fail with the error "Email has already been taken". I think what is happening is that when the test runs it creates a user with email "user@example.com" as suggested in the book. The problem is when the second test runs which needs to create a user, it is using the same "user@example.com" email address. I know there are workarounds I've seen using factory girl to开发者_C百科 create a sequence of email addresses but I shouldn't have to do this to get the example from the tutorial working properly.
Has anyone else run into this problem? Judging by the lack of questions on this particular topic I am thinking it is a bug in my code but maybe someone else encountered this.
Doh! The problem was a commented line:
config.use_transactional_fixtures = true
in spec_helper.rb!
Newbie mistake.
An useful asset to clean your db for yours tests:
https://github.com/bmabey/database_cleaner
You should search on topic to how to clean database after each test. Issue may be as well caused by default user you load from fixtures and then when you create hin in the test again, but if you say 90% it seems like the first case. I do not attach links because it makes a difference when you use rspec, test::unit or cucumber, each of them has its perks.
Happy coding!
I've had this problem recently as well. The test DB should be cleaned after tests but for some reason it wasn't. All I did was run 'rake db:reset' to reset everything and then make sure that I was using factories (Factory Girl). Just try using either fixtures or factories so you know for sure you aren't actually hitting the database.
I saw similar behavior near the end of CH 11. My spec_helper.rb was correct. This fixed it for me:
restarted "rails s"
restarted autotest
rake db:reset
rake db:migrate
rake db:test:prepare
rake db:populate
HTH, Perry
精彩评论