简体   繁体   English

具有指定参数的RSpec测试方法

[英]RSpec Testing Method with named arguments

I am try testing a method call that receive a named argument, like this: 我正在尝试测试接收命名参数的方法调用,如下所示:

expect(@fake_task_search).to receive(:search).with({:query=>"a"})
        @repo.all({query:  "a"})

And the SUT 和SUT

def all(params)
  @search_task.search(query: params[:query]).load
end

When I ran this I receive this:w rong number of arguments (0 for 1). 当我运行此命令时,我收到此错误消息:w错误的参数数量(0表示1)。

Any help will be great. 任何帮助都会很棒。

Thanks 谢谢

Call matcher the same way you call method .with(query: "a") 调用匹配器的方式与调用方法.with(query: "a")

class Repo
  def initialize(search_task)
    @search_task = search_task
  end

  def all(params)
    @search_task.search(query: params[:query])
  end
end

it "calls" do
  @search_task = SearchTask.new
  @repo = Repo.new(@search_task)

  expect(@search_task).to receive(:search).with(query: "a")

  @repo.all({query:  "a"})
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM