【发布时间】:2026-01-30 22:45:01
【问题描述】:
我已经搜索了很多,但无法弄清楚,尽管它看起来很基本。这是我想要做的简化示例。
创建一个做某事但不返回任何内容的简单方法,例如:
class Test
def test_method(param)
puts param
end
test_method("hello")
end
但在我的 rspec 测试中,我需要传递不同的参数,例如“再见”而不是“你好”。我知道这与存根和模拟有关,我查看了文档但无法弄清楚:https://relishapp.com/rspec/rspec-mocks/v/3-0/docs/method-stubs
如果我这样做:
@test = Test.new
allow(@test).to_receive(:test_method).with("goodbye")
它告诉我存根一个默认值,但我不知道如何正确地做到这一点。
错误信息:
received :test_method with unexpected arguments
expected: ("hello")
got: ("goodbye")
Please stub a default value first if message might be received with other args as well.
我正在使用 rspec 3.0,并调用类似
@test.stub(:test_method)
不允许。
【问题讨论】:
-
您的错误信息似乎与您的存根相反。
标签: ruby rspec mocking stubbing