如何使用RSPEC调用其他方法来测试非静态方法
我正在尝试测试一种通过另一种方法调用的方法。 我不想测试其他方法的作用,因为这是一个单独的单元测试。
假设我有类似的东西:
class MyService
def method_a
a = 1
b = method_b
return a + b
end
def method_b
return 2
end
end
现在,我想测试Method_a-我想验证Method_b已执行。
我知道,如果这些方法是静态的,这应该有效。但就我而言,这不是静态的。
allow(MyService).to receive(:method_b)
我一直遇到这个错误: myService不实现method_b
我知道这是因为该方法不是静态的,但是我在文档中找不到任何适合我用例的方法。
I'm trying to test a method being called by another method.
I don't want to test what the other method do, because this is a separate unit test.
so let's say I have something like:
class MyService
def method_a
a = 1
b = method_b
return a + b
end
def method_b
return 2
end
end
Now, I want to test method_a - I want to verify that method_b was executed.
I know that this should work if the methods were static. But in my case, it's not static.
allow(MyService).to receive(:method_b)
I keep getting this error:MyService does not implement method_b
And I understand that's because the method is not static, but I can't find anything in the documentation that fit my use case.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为主要问题是您期望将类方法称为而不是实例
I think main problem problem is that you expecting for class method to be called and not instance