【问题标题】:Minitest: Integration test, Template pattern and NotImplementedErrorMinitest:集成测试、模板模式和 NotImplementedError
【发布时间】:2015-02-22 03:53:28
【问题描述】:

假设我有以下课程:

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

我想编写一个向 API 提交请求的集成测试。但是,我需要定义 config_info 来执行此操作。这是我为完成这项工作所做的尝试之一:

# 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

puts 语句导致NotImplementedError

  • 在这种情况下是否有更好的方法来编写集成测试(即不定义SpecificClass)?

  • 为什么方法查找路径中没有SpecificClass'实现config_info

  • 有没有更好的方法来强制最终用户提供发出 API 请求所需的配置信息?

现在我将简单地测试APIRequestClass.send_request 方法并传入适当的哈希值。我对上面提出的问题仍然很感兴趣,但也许第四个问题是:从AbstractClass 一路测试此功能与从APIRequestClass 简单测试相比,是否有显着优势?

【问题讨论】:

    标签: ruby integration-testing minitest


    【解决方案1】:

    从 AbstractClass 一路测试这个功能,而不是从 APIRequestClass 简单地测试它有显着优势吗?

    看起来唯一被测试的是抽象类的未实现方法,所以也许不是?从某种角度来看,抽象类是具体类的一个实现细节,所以也许改为测试具体类更有意义。

    有没有更好的方法来强制最终用户提供发出 API 请求所需的配置信息?

    似乎配置和数据可能是两个独立的,send_request 的必需参数,它们在方法中组合在一起。这可能会让你完全摆脱抽象类。

    在这种情况下是否有更好的方法来编写集成测试(即不定义特定类)

    根据上述内容,您可以测试配置和数据是否已正确组合到 APIRequestClass 上的 API 有效负载中。那时可能有也可能没有任何东西要在特定类上进行测试。

    【讨论】:

    • 我需要抽象类,因为它是一个抽象,将在众多实现中使用,每个实现在config_info 上都有自己的排列。是的,我认为测试APIRequestClass 是最有意义的。谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-08
    • 2012-04-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多