使用Minitest测试对象在Ruby中调用私有方法

时间:2017-11-10 13:54:37

标签: ruby testing minitest

鉴于以下ruby代码,我想确保私有方法start仅在应用程序已配置为运行它时才启动。我认为这意味着我需要模拟类并存储私有方法,但我只是不把它放在一起。谁有能力推我?

module Work
  class Thing
    def initialize(value:)
      @local_state = value
      if Rails.configuration.report_loop
        start
      end
    end

    def change(new_value)
      @local_state = new_value
    end

    private

    def start
      Thread.new {perform_reporting}.tap do |thread|
        thread.abort_on_exception = true if thread
      end
    end

    def perform_reporting
      loop do
        sleep 5
        log @local_state
      end
    end
end

以下是我正在尝试的无效工作

def test_it_starts_reporting_automatically
  mock = Minitest::Mock.new
  mock.expect :start, true
  Work::Thing.stub :start, mock do
      hb = Work::Thing.new value: "test"
  end
  assert_mock mock
end

0 个答案:

没有答案