Ask A Question

Notifications

You’re not receiving notifications from this thread.

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

Simon Moro asked in Testing

Hi!

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(File.read(file_path)).with_indifferent_access
end


plan.rb - model

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

def self.all
plans.map { |plan| OpenStruct.new(plan) }
end

def self.where(args)
conditions = args.map do |arg_key, arg_val|
proc { |plan| plan[arg_key] == arg_val }
end
local_plans = plans.select { |plan| conditions.all? { |c| c.call(plan) } }
local_plans.flatten.map { |plan| OpenStruct.new(plan) }
end

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

Reply

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

Reply
Join the discussion
Create an account Log in

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

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

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