如何断言某些方法是用Ruby minitest框架调用的? [英] How to assert certain method is called with Ruby minitest framework?

查看:56
本文介绍了如何断言某些方法是用Ruby minitest框架调用的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想测试一个函数是否使用minitest Ruby正确调用了其他函数,但是我找不到一个合适的assert可以从

I want to test whether a function invokes other functions properly with minitest Ruby, but I cannot find a proper assert to test from the doc.

class SomeClass
  def invoke_function(name)
    name == "right" ? right () : wrong ()
  end

  def right
    #...
  end

  def wrong
    #...
  end
end

测试代码:

describe SomeClass do
  it "should invoke right function" do
    # assert right() is called
  end

  it "should invoke other function" do
    # assert wrong() is called
  end
end

推荐答案

使用minitest,您可以使用expect方法设置对像这样的模拟对象上调用的方法的期望

With minitest you use expect method to set the expectation for a method to be called on a mock object like so

obj = MiniTest::Mock.new
obj.expect :right

如果要使用参数设置期望值并返回值,则:

If you want to set expectation with parameters and return values then:

obj.expect :right, return_value, parameters

对于这样的具体对象:

obj = SomeClass.new
assert_send([obj, :right, *parameters])

这篇关于如何断言某些方法是用Ruby minitest框架调用的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆