Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

version 0.10.0. fixed bug in wrapper. rspec methods are deep_* now. d…

…oc and gemspec fixes
  • Loading branch information...
commit a6a406c07f5c3c9bc049493128abff642ebe8d31 1 parent 4a5fe58
@garysweaver authored
View
42 README.md
@@ -95,9 +95,9 @@ Put this in your `spec/spec_helper.rb`:
require 'stepford/factory_girl_rspec_helpers'
-Then you can just use `create`, `create_list`, `build`, `build_list`, or `build_stubbed` in your rspec tests (`create` becomes a shortcut for `::Stepford::FactoryGirl.create`, etc.), e.g.:
+Then you can just use `deep_create`, `deep_create_list`, `deep_build`, `deep_build_list`, or `deep_build_stubbed` in your rspec tests (`deep_create` becomes a shortcut for `::Stepford::FactoryGirl.create`, etc.), e.g.:
- create(:foo)
+ deep_create(:foo)
##### Stopping Circular References
@@ -108,6 +108,12 @@ If you have a circular reference (A has NOT NULL foreign key to B that has NOT N
[:waiter, :bar] => {on_loop: 2, set_to: Waiter.find(123)},
}
+##### Debugging
+
+Add somewhere after the require:
+
+ Stepford::FactoryGirl.debug = true
+
#### CLI
Stepford has a CLI with a circular reference checker and a generator to automatically create your factories file(s).
@@ -222,7 +228,37 @@ Currently uniqueness constraints are ignored and must be handled by FactoryGirl
###### Testing Factories
-See [Testing all Factories (with RSpec)][test_factories] in the FG wiki.
+See [Testing all Factories (with RSpec)][test_factories] in the FactoryGirl wiki.
+
+Here are a few rspecs that test the FactoryGirl factories and the Stepford deep_builds:
+
+ require 'spec_helper'
+ require 'stepford/factory_girl_rspec_helpers'
+
+ describe 'validate factories build' do
+ FactoryGirl.factories.each do |factory|
+ context "with factory for :#{factory.name}" do
+ subject { build(factory.name) }
+
+ it "is valid" do
+ subject.valid?.should be, subject.errors.full_messages
+ end
+ end
+ end
+ end
+
+ describe 'validate factories deep build' do
+ FactoryGirl.factories.each do |factory|
+ context "with factory for :#{factory.name}" do
+ subject { deep_build(factory.name) }
+
+ it "is valid" do
+ subject.valid?.should be, subject.errors.full_messages
+ end
+ end
+ end
+ end
+
##### Troubleshooting
View
19 lib/stepford/factory_girl.rb
@@ -31,6 +31,7 @@ module Stepford
# }
module FactoryGirl
OPTIONS = [
+ :debug,
:stop_circular_refs
]
@@ -39,7 +40,7 @@ class << self
def configure(&blk); class_eval(&blk); end
def method_missing(m, *args, &block)
- puts "starting Stepford::FactoryGirl.#{m}(#{args.inspect})"
+ puts "handling Stepford::FactoryGirl.#{m}(#{args.inspect})" if ::Stepford::FactoryGirl.debug?
if [:build, :build_list, :build_stubbed, :create, :create_list].include?(m) && args && args.size > 0
# call Stepford::FactoryGirl.* on any not null associations recursively
@@ -87,16 +88,16 @@ def method_missing(m, *args, &block)
end
with_factory_options[:circular_ref_counts][circular_ref_key] = count
if count > 100
- puts "over 100 loops. need to add this to Stepford::FactoryGirl.stop_circular_refs: {#{circular_ref_key.inspect} => {on_loop: 2, set_foreign_key_to: -1}}"
+ puts "over 100 loops. run: bundle exec stepford circular to find circular dependencies, then either change related NOT NULL columns to nullable and/or remove presence validators for related associations, or use Stepford::FactoryGirl.stop_circular_refs, e.g. #{circular_ref_key.inspect} => {on_loop: 2, set_foreign_key_to: -1}" if ::Stepford::FactoryGirl.debug?
end
if count >= (circ_options[:on_loop] || 1)
if circ_options.has_key?(:set_to)
- puts "Circular dependency override enabled. Returning :set_to instance to set as #{model_sym}.#{assc_sym}. instance was #{circ_options[:set_to]}"
+ puts "Circular dependency override enabled. Returning :set_to instance to set as #{model_sym}.#{assc_sym}. instance was #{circ_options[:set_to]}" if ::Stepford::FactoryGirl.debug?
return circ_options[:set_to]
elsif circ_options.has_key?(:set_foreign_key_to)
# (CHILD) return hash to set on parent
- puts "Circular dependency override enabled. Returning :set_foreign_key_to to set as #{model_sym}.#{reflection.foreign_key}. value was '#{circ_options[:set_foreign_key_to]}'"
+ puts "Circular dependency override enabled. Returning :set_foreign_key_to to set as #{model_sym}.#{reflection.foreign_key}. value was '#{circ_options[:set_foreign_key_to]}'" if ::Stepford::FactoryGirl.debug?
return {reflection.foreign_key.to_sym => circ_options[:set_foreign_key_to]}
end
end
@@ -108,10 +109,10 @@ def method_missing(m, *args, &block)
method_options = args.last
blk = method_options.is_a?(Hash) ? method_args_and_options.delete(:blk) : nil
if blk
- puts "FactoryGirl.__send__(#{method_args_and_options.inspect}, &blk)"
+ puts "FactoryGirl.__send__(#{method_args_and_options.inspect}, &blk)" if ::Stepford::FactoryGirl.debug?
options[assc_sym] = ::FactoryGirl.__send__(*method_args_and_options, &blk)
else
- puts "FactoryGirl.__send__(#{method_args_and_options.inspect})"
+ puts "FactoryGirl.__send__(#{method_args_and_options.inspect})" if ::Stepford::FactoryGirl.debug?
options[assc_sym] = ::FactoryGirl.__send__(*method_args_and_options)
end
else
@@ -138,8 +139,8 @@ def method_missing(m, *args, &block)
# (PARENT) we passed this back as a hash which means that the child model needed to set foreign key on the parent model
if options[assc_sym].is_a?(Hash)
value = options.delete(assc_sym)
- options.merge!()
- puts "Overrode foreign key #{model_sym}.#{assc_sym} = #{value}"
+ options.merge!(value)
+ puts "Overrode foreign key #{model_sym}.#{assc_sym} = #{value}" if ::Stepford::FactoryGirl.debug?
end
end
end
@@ -150,7 +151,7 @@ def method_missing(m, *args, &block)
# clean-up before sending to FactoryGirl
(args.last).delete(:with_factory_options) if args.last.is_a?(Hash)
- puts "FactoryGirl.#{m}(#{args.inspect}) (via __send__)"
+ puts "FactoryGirl.#{m}(#{args.inspect}) (via __send__)" if ::Stepford::FactoryGirl.debug?
::FactoryGirl.__send__(m, *args, &block)
end
end
View
2  lib/stepford/factory_girl_rspec_helpers.rb
@@ -4,7 +4,7 @@ module Stepford
module FactoryGirlRspecHelpers
[:create, :create_list, :build, :build_list, :build_stubbed].each do |s|
class_eval %Q"
- def #{s}(*args, &block)
+ def deep_#{s}(*args, &block)
::Stepford::FactoryGirl.#{s}(*args, &block)
end
"
View
2  lib/stepford/version.rb
@@ -1,3 +1,3 @@
module Stepford
- VERSION = '0.9.3'
+ VERSION = '0.10.0'
end
View
4 stepford.gemspec
@@ -8,8 +8,8 @@ Gem::Specification.new do |s|
s.authors = ['Gary S. Weaver']
s.email = ['garysweaver@gmail.com']
s.homepage = 'https://github.com/garysweaver/stepford'
- s.summary = %q{Autocreate FactoryGirl factories from models.}
- s.description = %q{A CLI to create starter FactoryGirl factories for all of your Rails models.}
+ s.summary = %q{FactoryGirl becomes easier and automated.}
+ s.description = %q{Has deep_* methods for automating FactoryGirl creation with required association trees and small tweaks, ActiveRecord circular required reference checker CLI, and a nice flexible FactoryGirl factories code generator CLI.}
s.files = Dir['lib/**/*'] + ['Rakefile', 'README.md']
s.license = 'MIT'
s.add_dependency 'thor'
Please sign in to comment.
Something went wrong with that request. Please try again.