接收块作为参数的模拟方法
我有一个或多或少像这样的场景,
class A
def initialize(&block)
b = B.new(&block)
end
end
我正在对 A 类进行单元测试,我想知道 B#new 是否正在接收传递给 A#new 的块。我使用 Mocha 作为模拟框架。
是否可以?
I have a scenario more or less like this
class A
def initialize(&block)
b = B.new(&block)
end
end
I am unit testing class A and I want to know if B#new is receiving the block passed to A#new. I am using Mocha as mock framework.
Is it possible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我用 Mocha 和 RSpec 尝试了这一点,虽然我可以通过测试,但行为是不正确的。根据我的实验,我得出的结论是,验证区块是否已通过是不可能的。
问题:为什么要传递一个块作为参数?该区块的用途是什么?什么时候应该调用它?
也许这确实是您应该使用以下内容进行测试的行为:
I tried this with both Mocha and RSpec and although I could get a passing test, the behavior was incorrect. From my experiments, I conclude that verifying that a block is passed is not possible.
Question: Why do you want to pass a block as a parameter? What purpose will the block serve? When should it be called?
Maybe this is really the behavior you should be testing with something like:
我想你想要:
我知道这适用于 RSpec,如果 Mocha 不能处理我会感到惊讶
I think you want:
I know this works with RSpec, I'd be surprised if Mocha doesn't handle
B::new
是否屈服于该块,并且该块是否修改了yield
提供的参数?如果是,那么你可以这样测试:Is
B::new
yielding to the block and does the block modify a param thatyield
provides? If yes, then you can test it this way: