'Minitest: Integration test, Template pattern and NotImplementedError

Suppose I have the following class:

class AbstractClass
  # code omitted

  def get_from_api(data)
    APIRequestClass.send_request(config_info: config_info(data))
  end

  # force subclass to add method w/ proper config hash
  def config_info(data)
    raise NotImplementedError
  end
end

I want to write an integration test that submits a request to the API. However, I need to define config_info to do this. Here is one of my attempts at making this work:

# abstract_class_test.rb
# once this works I would remove class declaration to helper file
require_relative '../test_setup'

class SpecificClass < AbstractClass
  def config_info(data)
    {
      sample: "Foo",
      data:   data
    }
  end
end


class AbstractClassTests < MiniTest::Test

  def setup
    @instance = SpecificClass.new
  end

  def test_that_something_comes_back_from_api
    puts @instance.config_info("data")
    response = @instance.get_from_api("data")

    assert_equal jobs.class, Array
  end

end

The puts statement is resulting in a NotImplementedError.

  • Is there a better way to write an integration test in this instance (i.e. not defining a SpecificClass)?

  • Why isn't SpecificClass' implementation of config_info in the method lookup path?

  • Is there a better way to force the end user to provide the configuration information needed to make the API request?

For now I'll simply test the APIRequestClass.send_request method and pass in the appropriate hash. I'm still very interested in the questions I asked above, but perhaps a fourth question is: is there a significant advantage in testing this functionality all the way from AbstractClass, versus simply testing it from APIRequestClass?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source