【发布时间】:2019-06-07 17:43:11
【问题描述】:
我最近将我的应用程序从 Rails 3 升级到 Rails 4,并且正在尝试运行规范测试。我认为过去可以正常工作的代码(在我在这里之前)突然抛出一个错误。
错误:
1) Admin::ReviewsController while logged in #index should get index
Failure/Error: stub_search("product")
ArgumentError:
wrong number of arguments (1 for 2+)
# ./spec/support/searchkick_stub.rb:5:in `stub_search'
# ./spec/controllers/admin/reviews_controller_spec.rb:8:in `block (3 levels) in <top (required)>'
这里是 searchkick_stub.rb:
module SearchkickStub
def stub_search(model)
klass = model.to_s.camelize.constantize
klass.any_instance.stub(:reindex) { true }
klass.stub(:search) do |term, options|
options ||= {}
@search_term ||= term
@search_params ||= options.dup
response = {
'hits' => {
'total' => 0,
'hits' => [],
}
}
result_opts = {
# per Searchkick::Query.new
page: [options[:page].to_i, 1].max,
per_page: (options[:limit] || options[:per_page] || 100000).to_i,
padding: [options[:padding].to_i, 0].max,
load: options[:load].nil? ? true : options[:load],
# per Searchkick::Query.execute
includes: options[:include] || options[:includes],
json: !options[:json].nil?,
}
Searchkick::Results.new(klass, response, result_opts)
end
# Code that reindexes Products will reindex their Inventories too.
stub_search(:inventory) if model == :product
end
end
stub_search 的签名显然是针对单个参数的,而不是像错误声明那样的两个或更多。
这是我们在 reviews_controller_spec.rb 中使用 stub_search 的地方
describe ReviewsController do
include SearchkickStub
before do
stub_search(:product)
...
end
end
【问题讨论】:
-
你能给整个 SearchkickStub 吗?跟踪表明问题出在 searchkick_stub.rb 的第 5 行。在黑暗中拍摄,但产生错误的可能是对 stub_search 的不同调用?
-
另外,与您的直接问题无关,但即使是 Rails 4 现在也有些旧了。 Rails 4.2 本身很快就要报废了。如果您的目标是让您的应用程序保持最新状态,您可能需要考虑跳过一步直接使用 Rails 5 甚至 6。
-
已编辑。嗯。第五行调用“klass.stub(:search)”。谢谢,也许这会有所帮助。
-
相信我,我知道。但我们只是试图从 Wheezy 升级到 Jessie,以及 ruby 和 rails。就我们现在能够升级的东西而言。
标签: ruby-on-rails-4 rspec