StubSolr
Sunspot.session = Sunspot::Rails::StubSessionProxy.new(Sunspot.session)
is enough for most of cases but if you want more than allow_any_instance_of(Sunspot::Rails::StubSessionProxy::Search).to receive(:results).and_return(myExpectedResults)
this gem can be helpful. kaminari
for pagination and plain AR to mimic resutls.
this gem depends on 2.2.6
version of sunspot, sunspot_rails.
Installation
Add this line to your application's Gemfile:
gem 'stub_solr'
And then execute:
$ bundle
Or install it yourself as:
$ gem install stub_solr
Usage
test_helper
file should have require 'stub_solr'
. In your test file, add setup and teardown like this
class ActivitiesControllerTest < ActionDispatch::IntegrationTest
def setup
Sunspot.session = Sunspot::Rails::StubSessionProxy.new(
Sunspot.session, Activity.all.to_a.concat(Excercise.all.to_a)
)
super
end
def teardown
Sunspot.session = ::Sunspot.session.original_session
super
end
test 'search with filter' do
get search_activities_url(params: { visible: true, search: 'beer' })
expected = Activity.where(visible: true).where("title LIKE ?", "%beer%")
res = ActiveSupport::JSON.decode(response.body)["activities"]
res.size.must_equal expected.size
end
end
For searching, it finds all string / text fields for given classes and use them.
For range search like greater_than
, it uses array to check an attribute is for range search. by default, it's %w[time month created_at start end]
but you can change that.
def setup
Sunspot.session = Sunspot::Rails::StubSessionProxy.new(
Sunspot.session, Activity.all.to_a.concat(Excercise.all.to_a)
)
Sunspot.session.set_range_fields(%w[time month created_at departure arrival])
super
end
Development
After checking out the repo, run bin/setup
to install dependencies. Then, run rake test
to run the tests. You can also run bin/console
for an interactive prompt that will allow you to experiment.
To install this gem onto your local machine, run bundle exec rake install
. To release a new version, update the version number in version.rb
, and then run bundle exec rake release
, which will create a git tag for the version, push git commits and tags, and push the .gem
file to rubygems.org.
Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/jaigouk/stub_solr.
License
The gem is available as open source under the terms of the MIT License.