Search code examples
ruby-on-railsrubyrspecfactory-botdatabase-cleaner

RSpec tests with Factory Girl creating image


In a rails 4.2.0 app I'm using RSpec and Factory Girl for tests. In the app I am using Paperclip for image uploads. When I run the tests with a blank image the image is being placed in the public folder.

Is there any way I can use the database_cleaner gem to fix this?

So far I have this:

spec\support\factory_girl.rb

RSpec.configure do |config|
config.include FactoryGirl::Syntax::Methods
 config.before(:suite) do
  begin
    DatabaseCleaner.start
    FactoryGirl.lint
  ensure
    DatabaseCleaner.clean
  end
 end
end

spec\support\database_cleaner.rb

RSpec.configure do |config|

 config.before(:suite) do
  DatabaseCleaner.clean_with(:truncation)
 end

 config.before(:each) do
  DatabaseCleaner.strategy = :transaction
 end

 config.before(:each) do
  DatabaseCleaner.start
 end

 config.after(:each) do
  DatabaseCleaner.clean
 end
end

Solution

  • DatabaseCleaner is for cleaning the database, not the file system. You can remove the file yourself instead, e.g. in an after block.

    after(:each) { FileUtils.rm @file_path }