Save 36% for Black Friday! Learn more

All threads / How to create a minitest fixture for a non database model?
Ask A Question


You’re not receiving notifications from this thread.

How to create a minitest fixture for a non database model?

Simon Moro asked in Testing


Is there a way to create a fixture for a model that is not in the database? I built my app a few years back following a guide in which the Plans were loaded from a YML file and instantiated as ActiveRecord models, but not persisted in the database.

When I attempt to create the fixtures for Plans, I get:

ActiveRecord::StatementInvalid: PG::UndefinedTable: ERROR: relation "plans" does not exist

Because, of course, there IS no table for plans.

However, I still need the Plan model available in my tests.

Thanks in advance!

Here the app code - which works... Just need to create fixtures :)

plan_loader.rb - initializer

file_path = "config/plans.yml"
if File.exist?(file_path)
PLANS = YAML.load(

plan.rb - model

class Plan
def self.plans
@plans ||= { |k| PLANS[k] }

def self.all { |plan| }

def self.where(args)
conditions = do |arg_key, arg_val|
proc { |plan| plan[arg_key] == arg_val }
local_plans = { |plan| conditions.all? { |c| } } { |plan| }

def self.find(stripe_id)
plan = plans.find { |p| p[:stripe_id] == stripe_id }

Actually... it seems it is auo loading them into the test environment... So, consider this closed :)

Join the discussion

Want to stay up-to-date with Ruby on Rails?

Join 67,819+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.

    logo Created with Sketch.

    Screencast tutorials to help you learn Ruby on Rails, Javascript, Hotwire, Turbo, Stimulus.js, PostgreSQL, MySQL, Ubuntu, and more. Icons by Icons8

    © 2022 GoRails, LLC. All rights reserved.