Rails 3 Tutorial Chapter 11 “Validation failed: Email has already been taken” error

后端 未结 5 1306
伪装坚强ぢ
伪装坚强ぢ 2020-12-09 05:59

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 =&         


        
相关标签:
5条回答
  • 2020-12-09 06:29

    I had trouble with the Integration tests shortly after Chapter 9.4. All of my controller tests and the request integration test blew up with the message 'Email has already been taken'

    What I learned from RailsTutorial - chapter 8.4.3 - Test database not clearing after adding user in integration test is that you need to do something to clean up after integration tests, because unlike unit tests they may not clean up after themselves.

    The solution presented there was to use the DatabaseCleaner gem, the implementation of which is also explained in the linked Question.

    I think that if you don't implement some strategy for cleaning up after the integration test you will continue to have to use your 'shotgun' solution for cleaning up the DB every time you run the test suite. Definitely not fun.

    0 讨论(0)
  • 2020-12-09 06:32

    This works for me:

    bundle exec rake db:test:prepare

    It's also in the tutorial.

    0 讨论(0)
  • 2020-12-09 06:40

    For me, the issue seemed to be running Spork. After restarting it I can run the tests as many times as I need.

    0 讨论(0)
  • 2020-12-09 06:41

    In my case problem was in equals emails in

    factory :user
    

    and

    factory :user_with_additional_options
    
    0 讨论(0)
  • 2020-12-09 06:52

    It helped, added to the file factories

    sequence(:email) {|n| "person#{n}@example.com" }
    
    0 讨论(0)
提交回复
热议问题