Are there any factory rails to help me test fickle models?

I am working on a rails application where models are regular ruby โ€‹โ€‹classes without any level of stability (no active recording or similar). I want to test these models from RSpec with all the subtleties that some plants provide (driver, factory -girl). These models may become associated with persistent models in the future, or they may implement a custom sustainability model.

Any suggestions?

+3
source share
1 answer

I like to use Sham gem . As long as you have a fickle model that matches some elements of the base implementation, you should be fine. For example, in my Rails application, I would do the following:

# sham/dog_sham.rb
class Dog::Sham
  def self.options
    { name: "Barney" }
  end
end

# app/models/dog.rb
class Dog < Struct.new(:name)
  def self.create options
    self.new(options[:name])
  end
end

Then in the console, I can create a factory Dog using the sham command:

Sham::Config.activate!
Dog.sham!
=> #<struct Dog name="Barney">
0
source

All Articles