Skip to content

Instantly share code, notes, and snippets.

@agibralter
Created December 28, 2009 07:14
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save agibralter/264574 to your computer and use it in GitHub Desktop.
Save agibralter/264574 to your computer and use it in GitHub Desktop.
Turning off transactional fixtures for specific RSpec example groups and a ThinkingSphinx search use-case.
if Rails.env.features? || Rails.env.test?
ActiveRecord::Base.class_eval do
class << self
def delete_all_of_all
@model_subclasses_for_delete_all_of_all.each do |klass|
klass.delete_all
end
end
def inherited_with_model_tracker(subclass)
@model_subclasses_for_delete_all_of_all ||= []
@model_subclasses_for_delete_all_of_all << subclass
inherited_without_model_tracker(subclass)
end
alias_method_chain :inherited, :model_tracker
end
end
end
ENV['RAILS_ENV'] = 'features'
require File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'config', 'environment'))
require 'cucumber/rails/world'
require 'cucumber/formatter/unicode'
require 'cucumber/rails/rspec'
require 'webrat/integrations/rspec-rails'
# EmailSpec hack...
module EmailSpec
module Delayed
class Job
def self.work_off
# nothing
end
end
end
end
require 'email_spec/cucumber'
# Include factories and factory_girl's default step definitions.
require 'factories'
require 'factory_girl/step_definitions'
# Include ThinkingSphinx's test search helpers.
require 'cucumber/thinking_sphinx/external_world'
Cucumber::ThinkingSphinx::ExternalWorld.new
# Include feature helper files.
Dir[File.expand_path(File.join(File.dirname(__FILE__), 'helpers', '**', '*.rb'))].each { |f| require f }
ActionController::Base.allow_rescue = false
# Turn off transactions and instruct Cucumber to clean up the database
# manually.
Cucumber::Rails::World.use_transactional_fixtures = false
Before do
ActiveRecord::Base.delete_all_of_all
end
if defined?(Factory)
Factory.sequence :email do |n|
"bob-#{n}@foo.com"
end
Factory.sequence :name do |n|
"Mr. Bob #{n}"
end
Factory.define :user do |f|
f.email { Factory.next(:email) }
f.login { Factory.next(:name) }
f.password 'password'
f.password_confirmation { |user| user.password if user.password && !user.password_confirmation }
end
Factory.define :verified_user, :class => User do |f|
f.email { Factory.next(:email) }
f.login { Factory.next(:name) }
f.password 'password'
f.password_confirmation { |user| user.password if user.password && !user.password_confirmation }
f.after_create { |user| user.verify_email! }
end
# ...
end
require 'spec_helper'
describe User do
# Using "transactional fixtures" as usual...
describe "listing and searching" do
def populate
# Using factory girl...
@a = Factory(:user, :name => 'Bob')
@b = Factory(:user, :name => 'Sally')
@c = Factory(:user, :name => 'John')
@d = Factory(:user, :name => 'Dan')
end
describe "listing" do
before(:each) do
populate
end
it "should list by name" do
# Given a named_scope :by_name
User.by_name.should == [@a, @d, @c, @b]
end
# ...
end
# Can't use transactional fixtures when using TS to index...
describe_with_ts_enabled "searching" do
before(:each) do
populate
# only update sphinx indexes for User
update_indexes(User)
end
it "should search Bob" do
# use to_a because search returns a paginated Array subclass...
User.search('Bob').to_a.should == [@a]
end
# ...
end
end
# ...
end
ENV['RAILS_ENV'] = 'test'
require File.expand_path(File.join(File.dirname(__FILE__), '..', 'config', 'environment'))
require 'spec'
require 'spec/rails'
require 'spec/autorun'
require 'factories'
Dir[File.expand_path(File.join(File.dirname(__FILE__), 'support', '**', '*.rb'))].each { |f| require f }
Spec::Runner.configure do |config|
config.use_transactional_fixtures = true
config.use_instantiated_fixtures = false
config.fixture_path = "#{Rails.root}/spec/fixtures/"
config.include Webrat::Matchers, :type => :views
config.extend ControllerHelper, :type => :controller
config.include CustomSimpleMatchers
config.include MocksHelper
config.include ScopifyHelper
config.include SearchHelper
config.extend TransactionHelper
config.before(:all) do
ActionMailer::Base.delivery_method = :test
ActionMailer::Base.perform_deliveries = true
ActionMailer::Base.deliveries = []
end
config.before(:each, :behaviour_type => :controller) do
controller.instance_eval { flash.stub!(:sweep) }
end
end
module SearchHelper
def update_indexes(*models)
ThinkingSphinx::Test.index(*models.collect { |model| ["#{model.to_s.underscore}_core", "#{model.to_s.underscore}_delta"] }.flatten)
sleep(0.25)
end
module ClassMethods
def describe_with_ts_enabled(desc, &block)
before(:all) do
ThinkingSphinx::Test.start
end
after(:all) do
ThinkingSphinx::Test.stop
end
describe_without_transactional_fixtures(desc, &block)
end
end
def self.included(base)
base.extend(ClassMethods)
end
end
module TransactionHelper
def describe_without_transactional_fixtures(desc, &block)
describe "#{desc} (without transactional fixtures)" do
before(:all) do
ActiveSupport::TestCase.use_transactional_fixtures = false
end
after(:each) do
# Clean up manually with custom test/feature-only AR method.
ActiveRecord::Base.delete_all_of_all
end
instance_eval(&block)
after(:all) do
ActiveSupport::TestCase.use_transactional_fixtures = true
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment