如何测试在 rspec 中混合使用参数和关键字参数调用该方法?
How to test that method gets called with a mix of arguments and keyword arguments in rspec?
我有一个这样调用的方法:
post_signup(user,
"fb signup completed",
app_context: current_app_id,
description: "Automatically Populated Via #{current_app_id}")
参数是值和关键字参数的混合。我在测试中关心的是app_context。我尝试过类似的方法,但它不起作用:
it "should log an event with an app_context" do
expect(controller).to receive(:post_signup).with(hash_including(app_context: current_app_id))
subject
end
和
it "should log an event with an app_context" do
expect(controller).to receive(:post_signup).with(current_app_id, any_args)
subject
end
男人能做什么?
善良的公民不要绝望,hash_including
的魔法仍然会拯救你!
您似乎尝试将其作为第一个参数,但哈希实际上是 post_signup
方法的第二个参数。第一个参数是 user
.
因此,要使 expect
有效,您需要如下内容:
expect(controller).to receive(:post_signup).with(anything, hash_including(app_context: current_app_id))
所以任何东西都匹配 user
并且 hash_including
然后用于它的其余部分...
我有一个这样调用的方法:
post_signup(user,
"fb signup completed",
app_context: current_app_id,
description: "Automatically Populated Via #{current_app_id}")
参数是值和关键字参数的混合。我在测试中关心的是app_context。我尝试过类似的方法,但它不起作用:
it "should log an event with an app_context" do
expect(controller).to receive(:post_signup).with(hash_including(app_context: current_app_id))
subject
end
和
it "should log an event with an app_context" do
expect(controller).to receive(:post_signup).with(current_app_id, any_args)
subject
end
男人能做什么?
善良的公民不要绝望,hash_including
的魔法仍然会拯救你!
您似乎尝试将其作为第一个参数,但哈希实际上是 post_signup
方法的第二个参数。第一个参数是 user
.
因此,要使 expect
有效,您需要如下内容:
expect(controller).to receive(:post_signup).with(anything, hash_including(app_context: current_app_id))
所以任何东西都匹配 user
并且 hash_including
然后用于它的其余部分...