devise icon indicating copy to clipboard operation
devise copied to clipboard

Default fxtures violate UNIQUE constraint

Open gardnerapp opened this issue 2 years ago • 7 comments

Environment:

ruby 3.1.1 rails 7.0.3 devise 4.8.1

Reproduce: rails g devise:install && rails g devise user rails test

Result: All test fail because user fixtures fail unique constraint ActiveRecord::RecordNotUnique: RuntimeError: UNIQUE constraint failed: users.email fixtures: one: {} two: {}

Expected: Test run without error, fixtures generated with different emails

one:
      email: [email protected]

two:
     email: [email protected]

gardnerapp avatar Jun 09 '22 14:06 gardnerapp

Which part of Devise is responsible for generating fixtures? Could just run a system call with echo towards the fixture file but I feel like that is cheating.

gardnerapp avatar Jun 09 '22 14:06 gardnerapp

Devise just uses the Rails model generator for that: https://github.com/heartcombo/devise/blob/f8d1ea90bc328012f178b8a6616a89b73f2546a4/lib/generators/active_record/devise_generator.rb#L25

You technically get the same output if you just generate a User model yourself with Rails:

% rails g model user
      invoke  active_record
      create    db/migrate/20220609144317_create_users.rb
      create    app/models/user.rb
      invoke    test_unit
      create      test/models/user_test.rb
      create      test/fixtures/users.yml

% cat test/fixtures/users.yml
# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html

# This model initially had no columns defined. If you add columns to the
# model remove the "{}" from the fixture names and add the columns immediately
# below each fixture, per the syntax in the comments below
#
one: {}
# column: value
#
two: {}
# column: value

While we could technically replace the fixture with one of our own, I'm not sure there's much gain to it, if anyone is going to use the fixture they will likely be editing it with other values anyway.

carlosantoniodasilva avatar Jun 09 '22 14:06 carlosantoniodasilva

I could imagine better default fixtures / a documented template being useful for quick reference. Especially to see how to build a working fixture when we configure multiple modules (like confirmable). That's what brought me to this issue anyways.

ftambara avatar Oct 29 '22 00:10 ftambara

Normally when I generate a rails model it will populate the fixture with data: rails g model Foo bar bang invoke active_record create db/migrate/20221029225445_create_foos.rb create app/models/foo.rb invoke test_unit create test/models/foo_test.rb create test/fixtures/foos.yml

` cat test/fixtures/foo.yml one: bar: MyString bang: MyString

two: bar: MyString bang: MyString`

Not sure why the mock up values are not present for the Devise fixture. What we could do is create email fixtures with dummy emails [email protected] and [email protected]. Any suggestions for where to start?

gardnerapp avatar Oct 29 '22 22:10 gardnerapp