Module: Interface::TestHelper

Defined in:
lib/interface/test_helper.rb

Overview

Contains interface testing methods to include in your test framework

Instance Method Summary collapse

Instance Method Details

#assert_implements_interfaces(object, *interfaces) ⇒ Object Also known as: assert_implements_interface

Raises AssertionFailedError if object does not implement all methods from interfaces

interfaces defaults to object.interfaces if none are specified

Aliased as assert_implements_interface

Example

module Remote
  def on
  end

  def off
  end
end

module MockInterface
end

class BrokenDevice
  implements Remote, MockInterface
end

class BrokenDeviceTest < Test::Unit::TestCase
  def test_should_implement_interfaces
    assert_implements_interfaces BrokenDevice.new # Failure: unimplemented interface methods for BrokenDevice: {Remote=>["off", "on"]}

    # you can also explicitly pass interfaces to test
    assert_implements_interface BrokenDevice.new, MockInterface # passes
  end
end


35
36
37
38
39
# File 'lib/interface/test_helper.rb', line 35

def assert_implements_interfaces(object, *interfaces)
  interfaces = interfaces.empty? ? object.interfaces : interfaces.flatten
  unimplemented_methods = object.unimplemented_methods.reject { |interface, methods| !interfaces.include?(interface) }
  assert_block("unimplemented interface methods for #{object.class}: #{unimplemented_methods.inspect}") { unimplemented_methods.empty? }
end