Permalink
Browse files

Include DatabaseCleaner usage when demoing FactoryGirl.lint

Because built factories create associated records, the database may not
be empty when the suite is run. This encourages developers to start
DatabaseCleaner and clean after linting is complete to ensure a clean
database.

Closes #619, #611, #620
  • Loading branch information...
1 parent ea03790 commit 7f31ba9239fcad8e2fbbf19c84d6dd516e37dab5 @joshuaclayton joshuaclayton committed Feb 13, 2014
Showing with 9 additions and 1 deletion.
  1. +9 −1 GETTING_STARTED.md
View
10 GETTING_STARTED.md
@@ -88,11 +88,19 @@ RSpec.configure do |config|
# additional factory_girl configuration
config.before(:suite) do
- FactoryGirl.lint
+ begin
+ DatabaseCleaner.start
+ FactoryGirl.lint
+ ensure
+ DatabaseCleaner.clean
+ end
end
end
```
+After calling `FactoryGirl.lint`, you'll likely want to clear out the
+database, as built factories will create associated records.
+
Defining factories
------------------

8 comments on commit 7f31ba9

@gamafranco

Can confirm this is still happening.

@gamafranco

Based on @joshuaclayton's patch, this is a workaround, that can be placed in the spec_helper.rb:

config.before(:suite) do
begin
DatabaseCleaner.start
FactoryGirl.lint
ensure
DatabaseCleaner.clean
end
end

@mshytikov

Hi

I would say it is better to create separate spec file spec/lint_spec.rb with:

require 'rails_helper'

RSpec.describe "Lint" do
  it "FactoryGirl" do
    FactoryGirl.lint
  end
end

In this case the references to DatabaseCleaner not needed, because the default cleaning strategy will be applied (for example config.use_transactional_fixtures = true will work as well).
And you can run it only on purpose, ( but not before any test run which may don't use factories at all)

@joshuaclayton
thoughtbot, inc. member

@mshytikov I dig this idea - especially when there are other mechanisms for stubbing external requests and other similarly tricky issues. Thanks for sharing!

@AWaselnuk

Thanks a lot @mshytikov! Perfect solution. I also want to point out that it took me a bit of time to track down FactoryGirl.lint as the source of this problem, and even then it was on a hunch. @joshuaclayton maybe you could consider adding a warning to the lint section in the FactoryGirl Getting Started doc?

@joshuaclayton
thoughtbot, inc. member

@AWaselnuk did you have some text in mind that would've been helpful for you? If so, could you put together a PR and we can discuss there? I'm completely onboard with improving the experience!

@AWaselnuk

@joshuaclayton I just saw this on GETTING_STARTED.md master branch:

After calling FactoryGirl.lint, you'll likely want to clear out the database, as built factories will create
associated records. The provided example above uses the database_cleaner gem to clear out
the database; be sure to add the gem to your Gemfile under the appropriate groups.

That's perfect. It mentions the expected side effect of linting and provides a possible solution. Thanks!

@e2

@joshuaclayton, @mshytikov - that rspec example would be perfect as a default generated test in factory_girl_rails

Please sign in to comment.