【问题标题】:Why does thinking-sphinx work in development but not in my spec?为什么thinking-sphinx 在开发中有效,但在我的规范中无效?
【发布时间】:2014-07-01 07:02:19
【问题描述】:

这对我来说似乎很有趣:在开发模式下,TS 可以轻松处理查询,但在测试中出现错误。

这是我的测试:

describe 'GET #index' do
  subject { get :index }

  let!(:products) { Fabricate.times(10, :product) }

  before(:all) do
    ThinkingSphinx::Test.init
    ThinkingSphinx::Test.index
    # sleeping for some time
    ThinkingSphinx::Test.start
  end

  after(:all) do
    ThinkingSphinx::Test.stop
  end

  it { expect(subject).to render_template(:index) }
  it { expect{subject}.to change{ assigns(:search).try(:products) }.to match_array(products) }
end

这是我的控制器:

def index
  @search = Query::Products.new(params.merge({
    kind_cd: Product.product,
    contractor: current_contractor
  }))
end

这是我的服务对象:

class Query::Products

  def initialize(params)
    @params = params
    process_products
    process_categories # error in this method
    process_properties
    process_brands
    process_prices
  end

private

  def process_products
    # ... other code

    @product_facets = Product.facets(query_string, common_search_options)
  end

  def common_search_options
    {
      order: ordering,
      ranker: :bm25,
      with: {
        price_type_for_search => min_price_conditions..max_price_conditions,
        kind_cd: @params[:kind_cd],
        brand_id: [*@params[:brand_ids]],
        category_ids: categories_for_search,
        property_value_ids: [*@params[:property_value_ids]],
      }
    }
  end

  def query_string
    @params[:query].present? ? Riddle.escape(@params[:query] + '*') : ''
  end

  def price_type_for_contractor
    @price_type_for_contractor ||= PriceType.for(@params[:contractor])
  end

  def price_type_for_search
    @price_type_for_search ||= "price_type_#{price_type_for_contractor.id}".to_sym
  end

  def min_price_conditions
    @min_price_conditions ||= @params[:min_price_limit].try(:to_f) || Price.where(price_type: price_type_for_contractor).minimum(:value)
  end

  def max_price_conditions
    @max_price_conditions ||= @params[:max_price_limit].try(:to_f) || Price.where(price_type: price_type_for_contractor).maximum(:value)
  end

  def ordering
    (@params[:order_by] == 'new') ? :created_at : price_type_for_search
  end

  def categories_for_search
    @params[:category_ids] || @params[:category_id] || []
  end

  def process_categories
    category_ids = if @params[:available_category_ids]
      Category.find(@params[:available_category_ids])
    elsif @params[:category_id]
      @category = Category.find(@params[:category_id])
      @category.children.ids
    else
      @product_facets[:direct_category_id].keys # errors here, see @product_facets definition
    end

    @categories = Category.where(id: category_ids)
  end

我得到了错误

 Failure/Error: subject { get :index }
 ThinkingSphinx::SyntaxError:
   sphinxql: syntax error, unexpected AND, expecting CONST_INT or CONST_FLOAT or '-' near 'AND  AND `kind_cd` = 0 AND `sphinx_deleted` = 0 GROUP BY `sphinx_internal_class` ORDER BY `price_type_3` ASC LIMIT 0, 1000 OPTION ranker=bm25, max_matches=1000; SHOW META; SELECT *, @groupby, @count FROM `product_core` WHERE `price_type_3` BETWEEN  AND  AND `kind_cd` = 0 AND `sphinx_deleted` = 0 GROUP BY `direct_category_id` ORDER BY `price_type_3` ASC LIMIT 0, 1000 OPTION ranker=bm25, max_matches=1000; SHOW META; SELEC
./app/services/query/products.rb:76:in `process_categories'

在开发模式下,我没有收到错误消息。为什么我有这个问题?

【问题讨论】:

  • 在您的测试日志文件中,您能找到导致问题的 Sphinx 查询吗?
  • 如果你正在测试你的控制器,你真的应该模拟任何外部对象。提供模拟响应而不是来自服务对象的真实响应,并确保控制器基于该固定响应正确运行。其他任何东西都是集成测试,不适合在控制器规范中进行测试。
  • @pat,抱歉耽搁了。在哪里可以找到日志?
  • RAILS_APP/log/test.log - 在那里,您应该看到 Sphinx 的 SphinxQL 查询(看起来与 SQL 非常相似,但并不完全相同)。
  • @pat,感谢您的帮助。我解决了。请看答案

标签: ruby-on-rails ruby rspec rspec-rails thinking-sphinx


【解决方案1】:

我发现了我的麻烦!

错误出现在transactional fixtures。禁用它们解决了我的问题。

这里更详细:How to test ThinkingSphinx using RSpec

【讨论】:

    猜你喜欢
    • 2017-04-05
    • 1970-01-01
    • 2015-07-07
    • 1970-01-01
    • 1970-01-01
    • 2021-04-15
    • 2014-07-20
    • 2011-03-12
    • 2015-09-12
    相关资源
    最近更新 更多