如何测试一个方法在对象中另一个之后被调用?

问题描述 投票:1回答:1

我有以下课程:

class Foo
  def initialize(foobar:)
    @foobar = foobar
  end

  def call
    return if foobar.blank?
    do_something_here
  end
end

哪个在模型的after_commit回调中被调用:

included { after_commit :invoke_method_in_poro, on: %I[create update] }

private

def invoke_method_in_poro
  Foo.new(foobar: to_json).call
end

现在,当我尝试测试是否正在调用call时,我首先需要对新方法进行存根处理,因为出现此错误:

NameError: undefined method `call' for class `Foo'
Did you mean?  caller

使用此代码:

foo = Foo
new_mock = Minitest::Mock.new
new_mock.expect :call, nil
foo.stub(:call, new_mock) do
  create(:model)
end
mock.verify

我的问题是,我最后如何才能测试是否正在调用call

ruby-on-rails ruby minitest
1个回答
1
投票

您在这里遇到了Minitest的特殊性:当在方法上定义call时,似乎在定义对象时minitest堆栈中的某些东西试图调用它。

给出此测试设置:

© www.soinside.com 2019 - 2024. All rights reserved.