如何断言使用 Ruby minitest 框架调用某些方法?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10869417/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to assert certain method is called with Ruby minitest framework?
提问by steveyang
I want to test whether a function invokes other functions properly with minitest Ruby, but I cannot find a proper assertto test from the doc.
我想用 minitest Ruby 测试一个函数是否正确调用其他函数,但我无法assert从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
采纳答案by nas
With minitest you use expectmethod to set the expectation for a method to be called on a mock object like so
使用 minitest,您可以使用expectmethod 来设置在模拟对象上调用方法的期望,如下所示
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
And for the concrete object like so:
对于像这样的具体对象:
obj = SomeClass.new
assert_send([obj, :right, *parameters])
回答by jvalanen
Minitest has a special .expect :callfor checking if some method is called.
Minitest 有一个特殊.expect :call的方法来检查是否调用了某个方法。
describe SomeClass do
it "should invoke right function" do
mocked_method = MiniTest::Mock.new
mocked_method.expect :call, return_value, []
some_instance = SomeClass.new
some_instance.stub :right, mocked_method do
some_instance.invoke_function("right")
end
mocked_method.verify
end
end
Unfortunately this feature is not documented very well. I found about it from here: https://github.com/seattlerb/minitest/issues/216
不幸的是,此功能没有很好地记录。我从这里找到了它:https: //github.com/seattlerb/minitest/issues/216
回答by Jing Li
According to the given example, there is no other delegate class, and you want to make sure the method is called properly from the same class. Then below code snippet should work:
根据给定的示例,没有其他委托类,并且您希望确保从同一类正确调用该方法。然后下面的代码片段应该可以工作:
class SomeTest < Minitest::Test
def setup
@obj = SomeClass.new
end
def test_right_method_is_called
@obj.stub :right, true do
@obj.stub :wrong, false do
assert(@obj.invoke_function('right'))
end
end
end
def test_wrong_method_is_called
@obj.stub :right, false do
@obj.stub :wrong, true do
assert(@obj.invoke_function('other'))
end
end
end
end
The idea is to stub [method_expect_to_be_called]by returning a simple truevalue, and in the stub block assert it's indeed being called and returning the truevalue. To stub the other unexpected method is just to make sure that it's not being called.
这个想法是通过返回一个简单的真值来存根[method_expect_to_be_call],并在存根块中断言它确实被调用并返回真值。存根另一个意想不到的方法只是为了确保它没有被调用。
Note: assert_send won't work correctly. Please refer to official doc.
注意: assert_send 将无法正常工作。请参考官方文档。
In fact, below statement will pass, but doesn't mean it's working as expected:
事实上,下面的语句会通过,但并不意味着它按预期工作:
assert_send([obj, :invoke_function, 'right'])
# it's just calling invoke_function method, but not verifying any method is being called

