FactoryGirl, DatabaseCleaner, and duplicate entries when creating ata

Everytime ti try to do something with DatabaseCleaner, I’m really sorry… “When it’s good, it’s very very good, but when it’s bad, it’s Horrid.” <Grr I am really beginning to hate DatabaseCleanerr>

Here’s the deal. I have several models with various associations, one of which (the manager model) has an email attribute. Whatever I try, I wind up getting a ‘email has already been used’ error.
Models

Manager: attributes => email, name
Company: attributes => name
NewsArticle: attributes => title
FeaturedCompany: has_one Company; has_one NewsArticle

Factor Specs

require 'spec_helper'

puts "===== #{File.basename(__FILE__)} ====="
describe "Factories" do

  describe "Company factory" do
    it "creates a valid company" do
      expect(FactoryGirl.build :company).to be_valid
    end 
  end 

  describe "Article factory" do
    it "creates a valid article" do
      expect(FactoryGirl.build :article).to be_valid
    end 
  end 

  describe "FeaturedCompany factory" do
    it "creates a valid featured company" do
      company = FactoryGirl.create :company
      article = FactoryGirl.create :article
      expect(FactoryGirl.build :featured_company).to be_valid
    end 

    describe "NewsEntry factory" do
      manager = FactoryGirl.create :manager
      news_entry = FactoryGirl.create :news_entry, manager: manager
      expect(FactoryGirl.build :news_entry).to be_valid
    end 

  end 
end

Factories

FactoryGirl.define do
#  sequence :email do |n|
#    "person#{n}@example.com"
#  end
#  sequence :name do |n|
#    "person#{1}"
#  end

  factory :company do
    name Faker::Company.name
    ata_involvement 'testing support'
    fund_name "Testing fund!"
    image { Rack::Test::UploadedFile.new(File.join(Rails.root, 'spec', 'fixtures', 'testing.jpg')) }
  end 

  factory :article do
    page_name 'companies'
    heading 'This is a test article for a company'
    body 'Article text for this article'
  end 

  factory :featured_company do
    association :company
    association :article
  end 

  factory :news_entry do
    title 'Short news title'
    association :manager
    intro_text Faker::Lorem.sentence
    date Date.today
  end 

  factory :manager do
    sequence(:name) { |n| "manager_name#{n}" }
    password Faker::Internet.password
    sequence(:email) { |n| "manager#{n}@example.com"  }
  end 
end

When I run the specs, I get the following error message:

undefined method `expect' for #<Class:0x00000004afad58> (NoMethodError)

and the contents of my manager table is:

13|manager_name1|manager1@example.com|$2a$10$86335.T9SC6xZ3bya7basu8Vj088yAbBbB0kb2Ine2FVHIRhDqPMm|t
14|manager_name2|manager2@example.com|$2a$10$qnn5W/Gga97u39SKvZMOOOXLlghYQbpYpnBKiLpkTIIgdJbrhjAly|t

if I then rerun the specs, it fails with:

Validation failed: Email has already been taken

and the same records are still in the table

I have the standard configuration for DatabaseCleaner as specified on gihub. I believe I’m getting some kind of validation error with NewsEntries on the first round, but why is the database not cleaned out properly?

So this won’t solve your whole problem, but I think the undefined method on expect is because you’re running expect in a describe block rather than an it-do block. I have that problem all the time when I’m writing tests too quickly.

Of course, @geoffharcourt<sigh> I’ve done that before and just totally not been able to see it. Thank you!

Followup: Clearning up that problem, let the DatabaseCleaner problem shine through

config.use_transactional_fixtures = true

and changing that to ‘false’ cleaned up the whole problem

Thanks again for catching the problem!

Suspenders’ test harness has that line set to false. Glad to hear it all worked out.