Skip to content

Instantly share code, notes, and snippets.

@thhermansen
Created June 5, 2011 11:04
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save thhermansen/1008868 to your computer and use it in GitHub Desktop.
Save thhermansen/1008868 to your computer and use it in GitHub Desktop.
Sunspot rspec helper file
#
# Put this in spec/support/
# Will use a sunspot stub session as default in all tests.
#
# To actually test the search you'll need something like:
# describe "something", sunspot: true do
# ...some tests...
# end
#
# If you do this in your spec helper:
# RSpec.configure do |config|
# ...
# config.treat_symbols_as_metadata_keys_with_true_values = true
# end
#
# You can drop the true in the options to describe.
# describe "something", sunspot do
# ...some tests...
# end
# Stubs search for all specs
Sunspot.session = Sunspot::Rails::StubSessionProxy.new(Sunspot.session)
# Rspec hook for enable it on selected tests
RSpec.configure do |config|
config.before :sunspot => true do
Sunspot.session = Sunspot.session.original_session
Sunspot.remove_all!
end
config.after :sunspot => true do
Sunspot.session = Sunspot::Rails::StubSessionProxy.new(Sunspot.session)
end
end
#
# Make sunspot index right away in test environment.
#
module Sunspot
module Rails
module Searchable
module InstanceMethods
def solr_index
solr_index!
end
def solr_remove_from_index
solr_remove_from_index!
end
end
end
end
end
@thegrubbsian
Copy link

I think your before(:all) and after(:all) are inverted in the EnableSupport module.

@thhermansen
Copy link
Author

Why do you think that - does it not work? Before it should set it to the original non-stubbed version. Afterwards it should set it back to a stubbed version to make the rest of the test go faster as we don't need to index test-data usually.

Anyway; I would probably do this different now. I'll update the gist.

@blanchma
Copy link

blanchma commented Apr 4, 2012

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment