thoughtbot/factory_girl — pull request #633 by evilguc (2014-03-19 16:20:15+00:00)
Title: code example error in GETTING_STARTED.md fix

Example for integration with RSpec and ActiveSupport::Notification is not valid.

Here is original example:

```ruby
config.before(:suite) do
@factory_girl_results = {}
ActiveSupport::Notifications.subscribe("factory_girl.run_factory") do |name, start, finish, id, payload|
factory_name = payload[:name]
strategy_name = payload[:strategy]
@factory_girl_results[factory_name] ||= {}
@factory_girl_results[factory_name][strategy_name] ||= 0
@factory_girl_results[factory_name][strategy_name] += 1
end
end

config.after(:suite) do
puts @factory_girl_results
end
```

This will never give you expected results since that two blocks run in different contexts. For example `self` for the first block is `#` and for the second is `#` during single `rspec` run.
Instance variable `@factory_girl_results` is not a closure.

My fix looks like:

```ruby
factory_girl_results = {}
config.before(:suite) do
ActiveSupport::Notifications.subscribe("factory_girl.run_factory") do |name, start, finish, id, payload|
factory_name = payload[:name]
strategy_name = payload[:strategy]
factory_girl_results[factory_name] ||= {}
factory_girl_results[factory_name][strategy_name] ||= 0
factory_girl_results[factory_name][strategy_name] += 1
end
end

config.after(:suite) do
puts factory_girl_results
end
```

I just replaced instance variable with closure.

FYI: I did not provide test example for the issue because it's in documentation, but I'll do if it is necessary.