Skip to content

Commit

Permalink
Stub Sunspot session in test suite.
Browse files Browse the repository at this point in the history
  • Loading branch information
nybblr committed Jul 8, 2016
1 parent d448ba6 commit 64e3cff
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
12 changes: 12 additions & 0 deletions spec/rails_helper.rb
Expand Up @@ -6,6 +6,8 @@
require 'spec_helper'
require 'rspec/rails'
# Add additional requires below this line. Rails is not loaded until this point!
require 'sunspot/rails/spec_helper'
require_relative 'support/sunspot'

# Requires supporting ruby files with custom matchers and macros, etc, in
# spec/support/ and its subdirectories. Files matching `spec/**/*_spec.rb` are
Expand Down Expand Up @@ -54,4 +56,14 @@
config.filter_rails_from_backtrace!
# arbitrary gems may also be filtered via:
# config.filter_gems_from_backtrace("gem name")

config.before(:each) do |example|
::Sunspot.unstub_session! if example.metadata[:search]
end

config.after(:each) do |example|
::Sunspot.stub_session! if example.metadata[:search]
end
end

::Sunspot.stub_session!
35 changes: 35 additions & 0 deletions spec/support/sunspot.rb
@@ -0,0 +1,35 @@
require 'open-uri'

module Sunspot
def self.stub_session!
::Sunspot.session = ::Sunspot::Rails::StubSessionProxy.new(::Sunspot.session)
end

def self.unstub_session!
::Sunspot.session = ::Sunspot.session.original_session
wait_for_solr
::Sunspot.remove_all!
end

def self.wait_for_solr
return if @started

print 'Waiting for Solr (run test suite with `bin/test`)'

until solr_listening?
print '.'
sleep 0.1
end

@started = true
end

def self.solr_listening?
open(::Sunspot.config.solr.url).read
true
rescue OpenURI::HTTPError
true
rescue Errno::ECONNREFUSED
false
end
end

0 comments on commit 64e3cff

Please sign in to comment.