Git Product home page Git Product logo

sunspot-rails-tester's Introduction

sunspot-rails-tester

This gem allows you to "turn on" solr for certain portions of your tests. For the code that does not use solr, you would want to "stub" sunspot to avoid unneeded indexing.

Here is an example RSpec 2 spec_helper.rb:

$original_sunspot_session = Sunspot.session

RSpec.configure do |config|      
  config.before do
    Sunspot.session = Sunspot::Rails::StubSessionProxy.new($original_sunspot_session)
  end
  
  config.before :each, :solr => true do
    Sunspot::Rails::Tester.start_original_sunspot_session
    Sunspot.session = $original_sunspot_session
    Sunspot.remove_all!
  end
end

Let's go through what the above code does.

  • $original_sunspot_session stores the original sunspot session. By default, sunspot_rails uses the SessionProxy::ThreadLocalSessionProxy.

  • In the first before block, we set the session to a stub session for every example. Sunspot::Rails::StubSessionProxy is just a dummy class that skips indexing.

  • In the second before block, we use RSpec 2's metadata feature by adding :solr => true. Any example or example group with this metadata will run the original sunspot session. Sunspot::Rails::Tester.start_original_sunspot_session starts the solr instance if it's not running.

Here is an example spec that utilizes sunspot-rails-tester:

require 'spec_helper'

describe 'search page' do
  it 'highlights the active tab in the navigation' do
    # uses the stub session
  end
  
  it 'finds and displays a person', :solr => true do
    # uses actual solr - indexing will happen
  end
end

Spork

To get this gem to work with Spork, all you need to do is move the start_original_sunspot_session line out of the RSpec.configure block:

$original_sunspot_session = Sunspot.session
Sunspot::Rails::Tester.start_original_sunspot_session

RSpec.configure do |config|      
  config.before do
    Sunspot.session = Sunspot::Rails::StubSessionProxy.new($original_sunspot_session)
  end
  
  config.before :each, :solr => true do
    Sunspot.session = $original_sunspot_session
    Sunspot.remove_all!
  end
end

Thanks

The following articles served as guidance and inspiration for this gem:

License

sunspot-rails-tester is released under the MIT license. See LICENSE for details.

Copyright

Copyright (c) 2011 Justin Ko

sunspot-rails-tester's People

Contributors

dx7 avatar justinko avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.