SCENARIO
I have extracted a concern called Taggable
. It's a module that allows any model to support tagging. I have included this concern/module into models like User
, Location
, Places
, Projects
.
I want to write tests for this module, but don't know where to start.
QUESTION
1. Can I do isolation testing on the Taggable
concern?
In the example below the test fails because the test is looking for a dummy_class table
. I'm assuming it's doing this because of the has_many
code in Taggable
so as a result it expects 'DummyClass'
to be an ActiveRecord object.
# /app/models/concerns/taggable.rb
module Taggable
extend ActiveSupport::Concern
included do
has_many :taggings, :as => :taggable, :dependent=> :destroy
has_many :tags, :through => :taggings
end
def tag(name)
name.strip!
tag = Tag.find_or_create_by_name(name)
self.taggings.find_or_create_by_tag_id(tag.id)
end
end
# /test/models/concerns/taggable_test.rb
require 'test_helpers'
class DummyClass
end
describe Taggable do
before do
@dummy = DummyClass.new
@dummy.extend(Taggable)
end
it "gets all tags" do
@dummy.tag("dummy tag")
@dummy.tags.must_be_instance_of Array
end
end
Part of me thinks if I just test a model that has this module included inside of it like User
that's enough of a test. But I keep reading that you should test modules in isolation.
Looking for some guidance / strategy on what the right approach is.