我想模拟一个将返回整数1
的服务。在端点中它被称为
CompanyNameFetcher.new(params[:filters][:company_name].downcase).call
所有这行我想模拟接收1
这是我到目前为止所拥有的:
let(:company_name_fetcher) do
class_double CompanyNameFetcher
end
before do
allow(company_name_fetcher).to receive(:new).and_return { 1 }
end
有错误
Failure/Error: allow(company_name_fetcher).to receive(:new).and_return { 1 }
ArgumentError: wrong number of arguments (given 0, expected 1+)
最佳答案
I want to mock a service that will returns the integer 1. In an endpoint it's called
CompanyNameFetcher.new(params[:filters][:company_name].downcase).call
and all of this line I want to mock to receive 1
选项 1 -allow_any_instance_of
allow_any_instance_of(CompanyNameFetcher).to(
receive(:call).and_return(1)
)
选项 2 - 对构造函数进行 stub
mock_fetcher = instance_double(CompanyNameFetcher)
allow(CompanyNameFetcher).to(
receive(:new).and_return(mock_fetcher)
)
allow(mock_fetcher).to rececive(:call).and_return(1)
关于ruby-on-rails - Rspec 模拟服务并接收整数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58616857/