I'm stumped. I have a rails app (3.1.3) that I have been building where I recently added devise (2.0) in order to create a user model and the corresponding authentication that comes with it. After I installed devise and ran rake test, ALL of my tests threw the same error. Across both unit and functional tests I got an error that looks like this:
ERROR should get index (0.05s)
ActiveRecord::RecordNotUnique: PG::Error: ERROR: duplicate key value violates unique constraint "index_users_on_email"
DETAIL: Key (email)=() already exists.
: INSERT INTO "users" ("created_at", "updated_at", "id") VALUES ('2012-03-30 04:13:13', '2012-03-30 04:13:13', 298486374)
/Users/myname/.rvm/gems/ruby-1.9.2-p290@global/gems/activerecord-3.1.3/lib/active_record/connection_adapters/postgresql_adapter.rb:592:in `async_exec'
I don't understand why this would affect ALL of my tests, or what could be causing it. Has anyone seen something like this before? What am I doing wrong here?
Any advice / help would be much appreciated!
Thanks!
Just in case somebody comes to this question, the answer was provided by Cam Norgate in a comment.
The problem lies in the file test/fixtures/users.yml
one: {}
# column: value
#
two: {}
# column: value
It is attemting to create two empty users (one and two), without email. Just put a comment before the two uncommented lines.
Devise is expecting a unique email column and you're trying to save more than one user with email = nil. Check your test fixtures, you probably just forgot to set an email for your fixture users.
This error can happen for various reasons. It helps looking at the full backtrace of PG::Error: ERROR, you'll see some app/lib code that should be causing this error, for your specific use-case.
In my case it was because I was mixing devise login/logout spec helpers with my own custom logout via capybara (click on "log out") helpers in my specs suite. Devise then called some methods to update the user fields via update_tracked_fields! and at that point must have created an empty user tried to save.
Since this is spec helpers (using warden/rack etc) it happened out of the flow of my rails app so explains why I got a raw error back instead of hitting my model validations.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With