Update 3: It seems like this is specific to fixtures in a .yml.erb - even if I have no templated code, it seems like fixtures in a yml.erb file doesn't get loaded. Having a plain .yml file works. This likely has nothing to do with devise per se.
Note: see Update 3 annotations for relevant changes
I need to generate Devise users in my rails app. I notice that clearing the database and loading the fixtures loads all other fixtures, except the Devise users (Update 3: which is in a .yml.erb file).
I have seen this other thread, but I tried all the options there and still doesn't seem to load the fixtures.
# ../fixtures/users.yml.erb
user1:
email: [email protected]
name: user1
encrypted_password: <%= Devise.bcrypt(User, 'passw0rd!') %>
# also tried encrypted_password: User.new(password_salt: '$2a$10$PoBe1MvkoGJsjMVTEjKqge').send(:password_digest, 'somepassword')
admin: true
And from the console:
To clear the test db:
$ bundle exec rake db:schema:load RAILS_ENV=test
To load the fixtures into test db:
$ bundle exec rake db:fixtures:load RAILS_ENV=test
Run rails console in test (no users found, but other model fixtures, like App, are being loaded):
$ rails c test
Loading test environment (Rails 4.1.5)
irb(main):001:0> User.first
User Load (0.1ms) SELECT "users".* FROM "users" ORDER BY "users"."id" ASC LIMIT 1
=> nil
irb(main):002:0> App.first
App Load (0.1ms) SELECT "apps".* FROM "apps" ORDER BY "apps"."id" ASC LIMIT 1
=> #<App id: 953336129,...>
Update 1: Also tried passing in encrypted password generated from console, still no user records are found:
admin:
email: [email protected]
name: user1
encrypted_password: $2a$04$DR0.2yfWwD8AZlyeXx0gEuk2Qh.cNLF4cir0ZUB1iW7hwQhK/IfcC
admin: true
Update 2: It works when I rename fixtures file to users.yml. Renaming to users.yml.erb seems to be the culprit. BTW, the same behavior is seen (that is, it works with .yml, but not with yml.erb) on the console and from rake test
You should pass the password in plain text too. I am sure there is a User model validation errors preventing your fixture users from being created. Here's an example from my users fixture which works:
tom:
first_name: Tom
last_name: Test
email: [email protected]
password: 123greetings
encrypted_password: <%= User.new.send(:password_digest, '123greetings') %>
If it still fails, please check the log/test.log
file for errors and check for missing required fields or other validation rules you set in your User model.
Update: It turns out that author found the issue himself - used .yml.erb file extension rather than .yml which made rails bypass that fixtures file. ERB works in yml fixtures as rails runs the fixture file through ERB before parsing it.