【发布时间】:2017-10-03 23:59:21
【问题描述】:
为使用类的 gem 编写单元测试的最佳方法是什么?我编写了一个 gem,它在初始化期间执行包括外部集成的操作。在编写测试时,我不想初始化所有内容,我只想测试某些功能。
比如我有lib/foomon.rb:
class FooMon
def initialize
@log = Logger.new("app.log","daily")
pollingClient = SomePoller.new(remote_service_url)
pollingClient.poll do |response|
processResponse(response)
end
end
private
def processResponse(res)
{:response => res.body}
end
end
现在我想测试processResponse 函数。我采用的方法是删除private 修饰符,然后将代码从initialize 移动到新的setup 方法。然后应用程序的主要入口点调用 setup,但我的测试没有(它们只有 before 中的 @foomon = FooMon.new)。然后规范可以测试@foomon.processResponse
这是最好的方法吗?
【问题讨论】: