【发布时间】:2019-03-12 14:03:24
【问题描述】:
我有一个像下面这样的基本模型
class MyModel
def initialize(attrs)
@attrs = attrs
@rest_client = Some::REST::Client.new
end
def do_a_rest_call(some_str)
@rest_client.create_thing(some_str)
end
end
出于测试目的,我不希望 @rest_client 进行远程调用。相反,在测试环境中,我只想确保 @rest_client 在经过某些代码分支时被特定的 some_str 调用。
在理想的世界中,我会有一个类似于以下的断言:
expect(my_model_instance).to.receive(do_a_rest_call).with(some_str) 在测试中我将通过 some_str 以确保它是正确的。
使用 RSpec 3.8 和 Rails 5.2.2 的最佳方法是什么?
【问题讨论】:
-
我不知道“最好的方法”。 (你如何量化“最佳”?)但是,我使用webmock,这让我觉得这不是那么糟糕。
标签: ruby-on-rails unit-testing testing rspec