如何在rspec中使用参数和关键字参数的混合来调用该方法?

时间:2016-04-06 00:17:52

标签: rspec mocking

我有一个像这样调用的方法:

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

男人能做什么?

1 个答案:

答案 0 :(得分:2)

不要让好公民绝望,hash_including的魔力仍会拯救你!

看起来您尝试将其作为第一个参数,但哈希实际上是post_signup方法的第二个参数。第一个参数是user

为了使工作expect你需要像:

expect(controller).to receive(:post_signup).with(anything, hash_including(app_context: current_app_id))

所以任何内容都匹配user,然后hash_including会用于其余内容......