Search code examples
ruby-on-railsdatabasetestingsingle-table-inheritance

Minitest: table empty when using STI


I had a single-table inheritance data model, where CityPage < Destination. This worked fine.

Now that I have added a second type CountryPage < Destination, my tests fail. Specifically, CityPage.all returns an empty result even though I have CityPage fixtures (which have not been changed & worked perfectly before).

What am I doing wrong? Why is my test data not being generated?


Solution

  • When you have STI with more than one child model, you need to use one fixtures file named after the parent, and distinguish the individual fixtures using the type property (i.e. what STI does under the hood). So in my case, I needed to have /test/fixtures/destinations.yml, not /test/fixtures/{city,country}_pages.yml.

    Brief Googling indicates this has something to do with Minitest clearing the db after test runs - see comment below.