What is the best way to test a module that uses associations, with a dummy active record class?
module Outputable
extend ActiveSupport::Concern
included do
has_one :output, as: outputable
end
delegate :print, to: :output
end
To use this module in a class, you'd make a new ActiveRecord model:
class Program < ActiveRecord::Base
include Outputable
def initialize(output)
self.output = output
end
end
prog = Program.create(Output.create)
prog.print("hello world") # delegates to Output.print
I want to be able to use several types of classes, for example Program
, Log
, etc. But I have not defined these ActiveRecord models yet, I'd like to test with a dummy class in specs (similar to this SO question). I've tried the Temping gem, but maybe it is outdated, using Rails 4, Ruby 2.1.2 I get:
when trying to do DummyProgram.create(output)
from:
Temping.create :dummy_program do
include Outputable
def initialize(output)
self.output = output
end
end
describe Outputable
let(:output) { Output.create }
let(:prog) { DummyProgram.create(output) }
it "exposes Output methods" do
expect(output).to receive(:print)
prog.print("hello world!")
end
end
I get a strange undefined method '[]' for nil:NilClass
method for the DummyProgram.create(output)
. It works how ever if I do DummyProgram.new(output)
but I need it to be saved to the temporary db.
Has anyone encountered a way around this, or an alternative to temping
gem?
Update: For now, I'm going with manually creating and dropping a table:
describe Outputable do
let(:output) { Output.create }
let(:prog) { DummyProgram.create(output: output) }
class DummyProgram < ActiveRecord::Base
include Workflows::Outputable
end
before :all do
m = ActiveRecord::Migration.new
m.verbose = false
m.create_table :dummy_program do |t|
t.integer :workflow_id
t.string :workflow_type
t.timestamps
end
end
after :all do
m = ActiveRecord::Migration.new
m.verbose = false
m.drop_table :dummy_program
end
...
end