Sha256: 076daef389af18c4c96d9b8cb1807172549ed1516d0dfd8c7cb0de8ce2fdd52b

Contents?: true

Size: 1.34 KB

Versions: 5

Compression:

Stored size: 1.34 KB

Contents

module EvelpidonTestHelpers
  ##
  # Stubbing out Solr
  #
  # Taken from : http://timcowlishaw.co.uk/post/3179661158/testing-sunspot-with-test-unit
  module TestSunspot
    class << self
      attr_accessor :pid, :original_session, :stub_session, :server

      def setup
        TestSunspot.original_session = Sunspot.session
        Sunspot.session = TestSunspot.stub_session = Sunspot::Rails::StubSessionProxy.new(Sunspot.session)
      end
    end

    def self.included(klass)
      klass.instance_eval do
        def startup
          Sunspot.session = TestSunspot.original_session
          rd, wr = IO.pipe
          pid = fork do
            STDOUT.reopen(wr)
            STDERR.reopen(wr)
            TestSunspot.server ||= Sunspot::Rails::Server.new
            begin
              TestSunspot.server.run
            ensure
              wr.close
            end
          end
          TestSunspot.pid = pid
          ready = false
          until ready do
            ready = true if rd.gets =~ /Started\ SocketConnector/
            sleep 0.5
          end
          rd.close
        end

        def shutdown
          Sunspot.remove_all!
          Process.kill("HUP",TestSunspot.pid)
          Process.wait
          Sunspot.session = TestSunspot.stub_session
        end
      end

      def teardown
        Sunspot.remove_all!
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
evelpidon_test_helpers-0.3.1 lib/evelpidon_test_helpers/sunspot.rb
evelpidon_test_helpers-0.3.0 lib/evelpidon_test_helpers/sunspot.rb
evelpidon_test_helpers-0.2.0 lib/evelpidon_test_helpers/sunspot.rb
evelpidon_test_helpers-0.1.1 lib/evelpidon_test_helpers/sunspot.rb
evelpidon_test_helpers-0.1.0 lib/evelpidon_test_helpers/sunspot.rb