Create / recreate a migration-generated record in a test database with RSpec

I have a migration to Rails that inserts a record into a database. The category model depends on this entry. Since RSpec cleans the database before each example, this record is lost and, moreover, is never created, since RSpec does not seem to generate the database from migrations. What is the best way to create / recreate this entry in a database? Will it be used before (: all)?

+3
source share
2 answers

It’s not that RSpec cleans the database, it is that Rails rake: db: prepare task copies the schema (but not the contents) of your dev database to your * _test db.

, before(:all), , .

( : - , "dev" "test" . db , rake: db: . .)

+6

, db/migration, , yml, category.yml

def self.up
 down
 directory = File.join( File.dirname(__FILE__), "data" )
 Fixtures.create_fixtures( directory, "categories" )
end

def self.down
  Category.delete_all
end
+2

Source: https://habr.com/ru/post/1699254/


All Articles