【问题标题】:Issue using solr and rspec使用 solr 和 rspec 的问题
【发布时间】:2013-12-26 13:58:35
【问题描述】:

我有一个提示自动完成的方法。我也在使用 Solr。

控制器中的方法如下:

def address_autocomplete
    search = Sunspot.search(Address) do
      fulltext params[:term]
      with(:user_id, current_user.id)
    end.results
    render json: search.map(&:address_with_id_to_string).to_json
  end

我正在为上述方法编写 rspec。上述方法的测试如下:

require 'spec_helper'

describe AddressesController, type: :controller do
  describe Address, search: true do
    describe 'Search' do
      before(:each) do
        Sunspot.remove_all!
        Sunspot.commit
      end

      let!(:address) do
        create(:address,
               name: 'Michael Jackson',
               street: '100 spear st',
               postal_code: '94105',
               state_or_region: 'California',
               phone_number: '123-456-7890',
               city: 'San Francisco',
               user: user).tap { |b| b.index! }
      end

      describe 'GET #address_autocomplete' do
        let!(:user)  { create :user }
        it 'It prompts autocomplete of addresses' do
          xhr :get, address_autocomplete_addresses_path , term: 'Mic'
          debugger
          ActiveSupport::JSON.decode(response.body).to eq(address.address_with_id_to_string)
          //Check response is desired string or not
        end
      end
    end
  end
end

在上面的代码中,经过调试发现 Sunspot 无法在搜索数组中返回想要的响应。搜索数组为空。我找不到发生这种情况的原因。

【问题讨论】:

    标签: rspec autocomplete sunspot-rails


    【解决方案1】:

    问题不在于 solr。我正在集成测试文件夹中编写测试。这给我带来了很多问题。由于在集成测试文件夹中找不到设备助手,因此我也无法使用sign_in user。我将测试移至单元测试,现在可以正常工作了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-05-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-25
      • 2021-06-05
      • 1970-01-01
      相关资源
      最近更新 更多