0

I have a seeds.rb file like

User.create(name: "Lando")
User.create(name: "Vader")
if User.all.count < 2
  raise 'hell'
end

that if/raise block is there for testing, because I have been unable to see these entities appear in my database at the beginning of my RSpec tests.

I see the raise happen every time I do bin/rails db:reset.

I may be missing some point of how the seeding thing works, but how can I go about entering these entities persistently? I can see of course that the code is being run because that crash occurs (which is weird on its own), but I haven't found a way (spec, or like this) to assert in CI that the change I made is working the way I intend short of a very heavyweight test. Since I would like to make relatively complex assertions about how other code treats these entities, I need to be able to see them appear during normal controller tests.

qqq
  • 1,360
  • 1
  • 9
  • 24
  • The test environment has a different database. You can run `env RAILS_ENV=test rake db:seed`. However some people use a gem like database_cleaner to wipe their test db each time. You should check that you don't have this. – max pleaner Nov 10 '17 at 06:57
  • make db/seed as part of your rspec/mintest by - https://stackoverflow.com/a/39110709/1376448 – kiddorails Nov 10 '17 at 07:07
  • You might want to use `create!` (note the `!`) instead of `create` because `create!` will raise an error with a useful message if Rails was unable to create that record. Without an error message, my best guess is that a validation on `User` fails and therefore the record isn't persisted to the database. – spickermann Nov 10 '17 at 07:19

0 Answers0