Elmer
Elmer

Reputation: 31

rspec: Testing a module that is included in other classes

I have two classes A and B with common behavior. Let's say I put the common stuff in a module that each class includes:

class A
  include C

  def do_something
    module_do_something(1)
  end
end

class B
  include C

  def do_something
    module_do_something(2)
  end
end

module C
  def module_do_something(num)
    print num
  end
end

(To begin with, is this a reasonable way to structure the classes/modules? From a Java background, I would have made C an abstract class that A and B both inherit from. However, I've read that Ruby doesn't really have a notion of an abstract class.)

What is a good way to write tests for this?

Upvotes: 0

Views: 930

Answers (1)

randallreedjr
randallreedjr

Reputation: 340

Yes, this looks like a reasonable way to structure your code in Ruby. Typically, when mixing-in a module, you would define whether the module's methods are class or instance methods. In your example above, this could look like

module C
  module InstanceMethods
    def module_do_something(num)
      print num
    end
  end
end

Then in your other classes, you would specify

includes C::InstanceMethods

(includes is used for InstanceMethods, extends is used for ClassMethods)

You can create tests in rspec using shared examples.

share_examples_for "C" do
  it "should print a num" do
    # ...
  end
end

describe "A" do
  it_should_behave_like "C"

  it "should do something" do
    # ...
  end
end

describe "B" do
  it_should_behave_like "C"

  it "should do something" do
    # ...
  end
end

Example adopted from here. And here is another discussion site with some more info on shared examples.

Upvotes: 1

Related Questions