为什么思考 - 狮身人面像在开发中工作但在我的规范中不起作用?

时间:2014-07-01 07:02:19

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

对我来说这似乎很有趣:在开发模式下,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'

在开发模式下,我没有收到错误。为什么我有这个麻烦?

1 个答案:

答案 0 :(得分:0)

我发现了我的麻烦!

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

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