Search code examples
ruby-on-railsmongodbrspecfactory-botrspec-rails

How to resolve data append in factorygirl while testing?


I am new to rails. i try to write test for a model for that i use factory-girl gem. In that data was taken from XML file.

My problem is when ever am running my rspec file, data was appended every time, in XML file i have only 32 data, but every time am executing rsepc data was increasing...

i even tried database_cleaner but same result.

I want to delete the data in factory-girl.

is there anyway to avoid duplication in factory-girl?

is there anyway to use where condition like query for factory-girl?

Thank you.


Solution

  • Try this:

    The following things use to reset factory girl data.

    Add following line in your Gemfile and try bundle install.

    gem "database_cleaner", ">= 0.8.0", :group => :test
    

    In spec_helper.rb:

    RSpec.configure do |config|
      # Other things
    
      # Clean up the database
      require 'database_cleaner'
      config.before(:suite) do
        DatabaseCleaner.strategy = :truncation
        DatabaseCleaner.orm = "mongoid"
      end
    
      config.before(:each) do
        DatabaseCleaner.clean
      end
    end