如何检查一个方法是否从同一个 class 调用另一个方法?
How to check if a method calls another method from the same class?
我正在尝试使用 Minitest 为 class 设置单元测试。在这个class中,一些方法调用了其他方法。
这是我想要做的事情的一个非常简化的概念。
require 'minitest/autorun'
class Book
def caller
self.called
end
def called
nil
end
end
class BookTest < Minitest::Test
def test_checkCaller
fake = Minitest::Mock.new
fake.expect(:caller, nil)
fake.caller
assert(fake.verify,msg=nil)
end
end
这个returns:
Run options: --seed 24362
# Running:
.
Finished in 0.002716s, 368.1771 runs/s, 368.1771 assertions/s.
1 runs, 1 assertions, 0 failures, 0 errors, 0 skips
我什至不确定这是否符合我的要求。我想要的是确认 called
正在被 caller
.
调用
后面我也想测试一下,当我把某个消息发到caller
,然后某个代码被called
运行,比如把那个消息加到一个队列。
[在任何人将此标记为重复之前,请确保假定的 post 实际上与我所问的相同,并且最好对我的问题有一个合法的答案。我看过多个相似的 post,但每个都略有不同。]
谢谢!
我喜欢用一个叫做 spy
的 gem。通过这种方式,您基本上可以 spy 方法并查看它是否被调用。 https://github.com/ryanong/spy
我正在尝试使用 Minitest 为 class 设置单元测试。在这个class中,一些方法调用了其他方法。
这是我想要做的事情的一个非常简化的概念。
require 'minitest/autorun'
class Book
def caller
self.called
end
def called
nil
end
end
class BookTest < Minitest::Test
def test_checkCaller
fake = Minitest::Mock.new
fake.expect(:caller, nil)
fake.caller
assert(fake.verify,msg=nil)
end
end
这个returns:
Run options: --seed 24362
# Running:
.
Finished in 0.002716s, 368.1771 runs/s, 368.1771 assertions/s.
1 runs, 1 assertions, 0 failures, 0 errors, 0 skips
我什至不确定这是否符合我的要求。我想要的是确认 called
正在被 caller
.
后面我也想测试一下,当我把某个消息发到caller
,然后某个代码被called
运行,比如把那个消息加到一个队列。
[在任何人将此标记为重复之前,请确保假定的 post 实际上与我所问的相同,并且最好对我的问题有一个合法的答案。我看过多个相似的 post,但每个都略有不同。]
谢谢!
我喜欢用一个叫做 spy
的 gem。通过这种方式,您基本上可以 spy 方法并查看它是否被调用。 https://github.com/ryanong/spy