I have a method which prompts auto complete. I am using Solr for the same.
The method in the controller looks like:
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
I am writing rspec for the above method. The test for the above method is as shown below:
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
In the above code, after debugging I found that Sunspot is unable to return the desired response in search array. The search array is blank. I am unable to find the reason why this is happening.