Skip to content

Commit

Permalink
Include DatabaseCleaner usage when demoing FactoryGirl.lint
Browse files Browse the repository at this point in the history
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
joshuaclayton committed Feb 26, 2014
1 parent ea03790 commit 7f31ba9
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion GETTING_STARTED.md
Expand Up @@ -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
------------------

Expand Down

8 comments on commit 7f31ba9

@gamafranco
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can confirm this is still happening.

@gamafranco
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@AWaselnuk
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@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
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@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
Copy link

@e2 e2 commented on 7f31ba9 Jan 25, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Please sign in to comment.