I have a module like this (but more complicated):
module Aliasable
def self.included(base)
base.has_many :aliases, :as => :aliasable
end
end
which I include in several models. Currently for testing I make another module as below, which I just include in the test case
module AliasableTest
def self.included(base)
base.class_exec do
should have_many(:aliases)
end
end
end
The question is how do I go about testing this module in isolation? Or is the above way good enough. Seems like there is probably a better way to do it.
First off,
self.includedis not a good way to describe your modules, andclass_execis needlessly complicating things. Instead, you shouldextend ActiveSupport::Concern, as in:You didn’t mention what test framework you’re using, but RSpec covers exactly this case. Try this:
Assuming your models look like:
Then, in your tests, you can do: