← Back to Upcase

FactoryGirl, DatabaseCleaner, and duplicate entries when creating ata

(Jon Seidel) #1

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.

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

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

  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

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



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')) }

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

  factory :featured_company do
    association :company
    association :article

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

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

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:


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?

(Geoff Harcourt) #2

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.

(Jon Seidel) #3

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!

(Geoff Harcourt) #4

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