factory_girl_test_monitor 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: c8d49e4f70dca393d3048626971505a26a5d8dbf
4
+ data.tar.gz: 46f025e7dea3110429013b8a948eb3ae48e1af3d
5
+ SHA512:
6
+ metadata.gz: f76cf332b6f67cebb47fbde94cc955757a17421f2ffd88cbf4224706d2c8bacc6a548c759a13ef1c050583e2710056c33a19db1c1a148d845906a3853d16c0b4
7
+ data.tar.gz: 26da9ec9601cd26bbca9b56a19e0d47d9844e612d355cb7c8d389bc027ffcaed61b2fa37d2810faf28a691e71177f74c2fbefebd3ebff3828aee8907d0197782
@@ -0,0 +1,46 @@
1
+ ## FactoryGirl Test Monitor
2
+
3
+ Monitors FactoryGirl strategy invocations (e.g. build, create, etc.) per test with ActiveSupport Instrumentation as described in FactoryGirl's getting started doc in order to output the strategy invocations.
4
+
5
+ Tries to automatically integrate with MiniTest and/or RSpec.
6
+
7
+ Has a configurable threshold for strategy invocations before printing a warning message.
8
+
9
+ This may be very helpful if you are getting "SystemStackError: stack level too deep" errors caused by factory_girl factories. Those are not always trivial to track down without having more info about strategy invocations.
10
+
11
+ Depending on your reporter, etc. might look something like this in your test output:
12
+
13
+ FactoryGirlTestMonitor.strategy_invocation_max of 199 was exceeded: {:customer=>{:create=>1}, :contact=>{:create=>1}, :project=>{:create=>216}}
14
+ ...
15
+ SystemStackError: stack level too deep
16
+ /path/to/activesupport.../lib/active_support/notifications/instrumenter.rb:23
17
+
18
+ There you can see that the project factory is involved in 216 creates, which was more in this case than the intended number.
19
+
20
+ ### Setup
21
+
22
+ In your ActiveRecord/Rails 3.1+ project, add this to your Gemfile in your test group:
23
+
24
+ gem 'factory_girl_test_monitor'
25
+
26
+ Then run:
27
+
28
+ bundle install
29
+
30
+ ### Configuration
31
+
32
+ By default the max strategy invocations is 199. To reconfigure can do this in an initializer:
33
+
34
+ FactoryGirlTestMonitor.strategy_invocation_max = 199
35
+
36
+ ### Debug
37
+
38
+ To track down why support is not being added for minitest or rspec:
39
+
40
+ FactoryGirlTestMonitor.debug = true
41
+
42
+ ### License
43
+
44
+ Copyright (c) 2013 Gary S. Weaver, released under the [MIT license][lic].
45
+
46
+ [lic]: http://github.com/garysweaver/factory_girl_test_monitor/blob/master/LICENSE
@@ -0,0 +1,94 @@
1
+ require 'factory_girl'
2
+ require 'active_support/notifications'
3
+
4
+ module FactoryGirlTestMonitor
5
+ class << self
6
+ attr_accessor :strategy_invocation_max, :debug
7
+ end
8
+ end
9
+
10
+ FactoryGirlTestMonitor.strategy_invocation_max = 199
11
+
12
+ # MiniTest support
13
+ begin
14
+ require 'minitest/unit'
15
+
16
+ module FactoryGirlTestMonitor
17
+ module MiniTestFactoryStrategyInvocationCollector
18
+ def before_setup
19
+ @factory_girl_run_factories = {}
20
+ @factory_girl_max_invocations = 0
21
+ ActiveSupport::Notifications.subscribe("factory_girl.run_factory") do |name, start, finish, id, payload|
22
+ factory_name = payload[:name]
23
+ strategy_name = payload[:strategy]
24
+ @factory_girl_run_factories[factory_name] ||= {}
25
+ @factory_girl_run_factories[factory_name][strategy_name] ||= 0
26
+ @factory_girl_run_factories[factory_name][strategy_name] += 1
27
+ this_num = @factory_girl_run_factories[factory_name][strategy_name]
28
+ @factory_girl_max_invocations = [@factory_girl_max_invocations, @factory_girl_run_factories[factory_name][strategy_name]].max
29
+ end
30
+ super if defined?(super)
31
+ end
32
+ end
33
+ end
34
+
35
+ class MiniTest::Unit::TestCase
36
+ include ::FactoryGirlTestMonitor::MiniTestFactoryStrategyInvocationCollector
37
+ end
38
+
39
+ if defined? Minitest::Test
40
+ class Minitest::Test
41
+ alias_method :run_with_factory_girl_test_monitor, :run
42
+
43
+ def run
44
+ run_with_factory_girl_test_monitor
45
+ ensure
46
+ puts "\nFactoryGirlTestMonitor.strategy_invocation_max of #{FactoryGirlTestMonitor.strategy_invocation_max} was exceeded: #{@factory_girl_run_factories.inspect}" if defined?(@factory_girl_run_factories) && @factory_girl_max_invocations > FactoryGirlTestMonitor.strategy_invocation_max
47
+ end
48
+ end
49
+ else
50
+ class MiniTest::Unit::TestCase
51
+ alias_method :run_with_factory_girl_test_monitor, :run
52
+
53
+ def run(runner)
54
+ run_with_factory_girl_test_monitor(runner)
55
+ ensure
56
+ puts "\nFactoryGirlTestMonitor.strategy_invocation_max of #{FactoryGirlTestMonitor.strategy_invocation_max} was exceeded: #{@factory_girl_run_factories.inspect}" if defined?(@factory_girl_run_factories) && @factory_girl_max_invocations > FactoryGirlTestMonitor.strategy_invocation_max
57
+ end
58
+ end
59
+ end
60
+
61
+ rescue LoadError, NameError => e
62
+ if FactoryGirlTestMonitor.debug
63
+ puts "FactoryGirlTestMonitor debug: Attempt to add MiniTest support failed: {e.message}\n #{e.backtrace.join(" \n")}"
64
+ end
65
+ end
66
+
67
+ # RSpec support
68
+ begin
69
+ require 'rspec/expectations'
70
+ RSpec.configure do |config|
71
+ config.around(:each) do |t|
72
+ @factory_girl_run_factories = {}
73
+ @factory_girl_max_invocations = 0
74
+ ActiveSupport::Notifications.subscribe("factory_girl.run_factory") do |name, start, finish, id, payload|
75
+ factory_name = payload[:name]
76
+ strategy_name = payload[:strategy]
77
+ @factory_girl_run_factories[factory_name] ||= {}
78
+ @factory_girl_run_factories[factory_name][strategy_name] ||= 0
79
+ @factory_girl_run_factories[factory_name][strategy_name] += 1
80
+ this_num = @factory_girl_run_factories[factory_name][strategy_name]
81
+ @factory_girl_max_invocations = [@factory_girl_max_invocations, @factory_girl_run_factories[factory_name][strategy_name]].max
82
+ end
83
+ begin
84
+ t.run
85
+ ensure
86
+ puts "\nFactoryGirlTestMonitor.strategy_invocation_max of #{FactoryGirlTestMonitor.strategy_invocation_max} was exceeded: #{@factory_girl_run_factories.inspect}" if defined?(@factory_girl_run_factories) && @factory_girl_max_invocations > FactoryGirlTestMonitor.strategy_invocation_max
87
+ end
88
+ end
89
+ end
90
+ rescue LoadError, NameError => e
91
+ if FactoryGirlTestMonitor.debug
92
+ puts "FactoryGirlTestMonitor debug: Attempt to add RSpec support failed: {e.message}\n #{e.backtrace.join(" \n")}"
93
+ end
94
+ end
@@ -0,0 +1,3 @@
1
+ module FactoryGirlTestMonitor
2
+ VERSION = '0.0.2'
3
+ end
metadata ADDED
@@ -0,0 +1,90 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: factory_girl_test_monitor
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Gary S. Weaver
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-02-10 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '3'
20
+ - - "<"
21
+ - !ruby/object:Gem::Version
22
+ version: '5'
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ version: '3'
30
+ - - "<"
31
+ - !ruby/object:Gem::Version
32
+ version: '5'
33
+ - !ruby/object:Gem::Dependency
34
+ name: factory_girl
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: '3.2'
40
+ - - "<"
41
+ - !ruby/object:Gem::Version
42
+ version: '6'
43
+ type: :runtime
44
+ prerelease: false
45
+ version_requirements: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: '3.2'
50
+ - - "<"
51
+ - !ruby/object:Gem::Version
52
+ version: '6'
53
+ description: Monitors FactoryGirl strategy invocations (e.g. build, create, etc.)
54
+ per test with ActiveSupport Instrumentation as described in FactoryGirl's getting
55
+ started doc in order to output the strategy invocations.
56
+ email:
57
+ - garysweaver@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - README.md
63
+ - lib/factory_girl_test_monitor.rb
64
+ - lib/factory_girl_test_monitor/version.rb
65
+ homepage: https://github.com/garysweaver/factory_girl_test_monitor
66
+ licenses:
67
+ - MIT
68
+ metadata: {}
69
+ post_install_message:
70
+ rdoc_options: []
71
+ require_paths:
72
+ - lib
73
+ required_ruby_version: !ruby/object:Gem::Requirement
74
+ requirements:
75
+ - - ">="
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ required_rubygems_version: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ requirements: []
84
+ rubyforge_project:
85
+ rubygems_version: 2.2.1
86
+ signing_key:
87
+ specification_version: 4
88
+ summary: Help diagnose stack too deep errors related to factory_girl factories in
89
+ minitest/rspec tests automatically.
90
+ test_files: []