Search code examples
rubyrspecmixins

Unable to invoke mixin module method in spec


I have the following module:

# lib/translator/sms_command.rb
module Translator
  module SmsCommand
    def self.included(klass)
      puts "SmsCommand initialized in #{klass}"
    end
    def alias_command(name)

    end
  end
end

And the following spec:

# spec/translator/sms_command_spec.rb
require 'spec_helper'

class ExampleCommand
  include Translator::SmsCommand
  alias_command :x
end

module Translator
  describe SmsCommand do
    describe "#alias_command" do
      it "registers the class with the command registry" do
        Translator::SmsCommand.command_registry.should include_key :x
      end

    end
  end
end

Yes #alias_command does nothing at this stage but that's because I'm in the midst of developing it. However, when I run my spec I am seeing...

SmsCommand initialized in ExampleCommand

so the module is certainly being mixed in, however the spec barfs on the alias_command :x line in the ExampleCommand as if the alias_command method is never becoming available.

/spec/translator/sms_command_spec.rb:5:in `<class:ExampleCommand>': undefined method `alias_command' for ExampleCommand:Class (NoMethodError)

I could solve this problem through inheritance though I'd prefer the mixin module. What am I missing?


Solution

  • It's because it's not an include but an extend to access alias_command in your Class method

    class ExampleCommand
      extend Translator::SmsCommand
      alias_command :x
    end