RedBassett
RedBassett

Reputation: 3587

Ruby on Rails - Understanding Database Dependencies in Test

I've got some model code that calls to the database with a simple find():

@thing = Thing.find(id)

I have data seeded in the database for the test environment. If I open the console in test (rails c -e test), I can run Thing.find(1) and get a result fine, however when I run a test that calls the method shown above, it reports that it cannot find a record with the id of 1.

I assume I am misunderstanding the relationship between test seed data and the tests being run against that database. Why do I see seed sin the test DB but the test doesn't?

Upvotes: 1

Views: 106

Answers (1)

kykyi
kykyi

Reputation: 385

A more conventional way of doing this would be to create fixtures (or factories if using FactoryBot) and call these factories in your test setup. As previous answers have said, hardcoding test ID's will likely return RecordNotFound due to auto-incrementing.

Upvotes: 0

Related Questions