Skip to content

Instantly share code, notes, and snippets.

@mariovisic
Created March 22, 2013 04:56
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mariovisic/5219034 to your computer and use it in GitHub Desktop.
Save mariovisic/5219034 to your computer and use it in GitHub Desktop.
Do not allow Factory Girl usage in unit tests.
# spec/support/factory_girl.rb
# This file is here to limit the use of factories to only integration tests and not unit tests.
#
# If you really want to use a factory you can add the tag :factories to a test
#
module FactoryGirlBlocker
mattr_accessor :disabled
def self.with_disabled
self.disabled = true
yield if block_given?
self.disabled = false
end
end
class FactoryGirl::Evaluation
def initialize_with_blocker(*args)
if FactoryGirlBlocker.disabled == true
initialize_without_blocker(*args)
else
do_not_allow
end
end
def do_not_allow
raise %{Hi there friend:
It seems as though you've tried to use a Factory Girl factory inside a unit test.
This is probably not a good idea, you should consider removing it.
Using factories means that your tests are no longer isolated so you're not really unit testing anymore.
If you REALLY want to keep it in your test, tag you test with :factories
Any complaints should be directed to Mario Visic (@mariovisic)}
end
alias_method_chain :initialize, :blocker
end
RSpec.configure do |config|
config.around(:each, :type => :feature) do |example|
FactoryGirlBlocker.with_disabled { example.run }
end
end
RSpec.configure do |config|
config.before(:all, :factories => true) do |example|
FactoryGirlBlocker.disabled = true
end
end
RSpec.configure do |config|
config.after(:all, :factories => true) do |example|
FactoryGirlBlocker.disabled = false
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment