Created
May 6, 2014 06:07
-
-
Save drueck/11554143 to your computer and use it in GitHub Desktop.
Variation of Corey Haines' active_record_spec_helper.rb that supports FactoryGirl, Shoulda and MoneyRails
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require "simplecov" | |
| SimpleCov.start "rails" | |
| require "yaml" | |
| require "active_record" | |
| require "factory_girl_rails" | |
| require "shoulda-matchers" | |
| require 'money-rails' | |
| connection_info = YAML.load_file("config/database.yml")["test"] | |
| ActiveRecord::Base.establish_connection(connection_info) | |
| MoneyRails::Hooks.init | |
| RSpec.configure do |config| | |
| config.include FactoryGirl::Syntax::Methods | |
| config.before(:suite) do | |
| FactoryGirl.factories.clear | |
| FactoryGirl.find_definitions | |
| end | |
| config.around do |example| | |
| ActiveRecord::Base.transaction do | |
| example.run | |
| raise ActiveRecord::Rollback | |
| end | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Loading all Factory definitions with
FactoryGirl.find_definitionsdidn't work for me as it requires all my models to be loaded... e.g. if I'm testing the modelPostI'll onlyrequire 'post'and not require all my other models, but then FactoryGirl will crash because it will try to load e.g. the factoryusersand fail because it can't find the definition ofUser.My solution is just to forget about factory definitions within
active_record_spec_helperand load them manually within the individual model spec files. Here's an example:I'm still having issues with my associations as described in this StackOverflow question - upvotes are available!