FactoryGirl error: "Email has already been taken."
Asked Answered
U

1

6

I get an error (shown below) when using the factory below. It is temporarily fixed by running rake db:test:prepare, however, this only fixes it for one running of the test. What can I do for a more permanent fix? Should I be using database_cleaner? If so, can anyone offer any insight into setting it up (I am using RSpec, Capybara, Rails 4, Ruby 2)? Thanks!

I am using the following factory:

FactoryGirl.define do 
  factory :user do
    sequence(:username)   { |n| "Person #{n}" }
    sequence(:email)      { |n| "person_#{n}@example.com" }
    password "foobar"
    password_confirmation "foobar"

    factory :admin do
      admin true
    end
  end

  factory :key do
    sequence(:name)  { |n| "Key #{n}" }
    sequence(:description)  { |n| "This is key #{n}" }
    root_note "C"
    accidental "Natural"
    third "Major"
    user
  end

  factory :chord do
    root_note "C"
    accidental "Natural"
    third "Major"
    fifth ""
    extension ""
    sequence(:name) { |n| "Chord #{n}" }
    sequence(:description)  { |n| "This is chord #{n}" }
    user

    factory :submitted_chord do
      state 'Submitted'
    end

    factory :published_chord do
      state 'Published'
    end
  end
end

And I am getting the following error when the factories are used:

1) Key pages viewing keys on the pending keys page for an admin 
     Failure/Error: @admin = FactoryGirl.create(:admin)
     ActiveRecord::RecordInvalid:
       Validation failed: Email has already been taken, Email has already been taken, Username has already been taken
     # ./spec/requests/key_pages_spec.rb:150:in `block (5 levels) in <top (required)>'

  2) Key pages viewing keys on the pending keys page for an admin 
     Failure/Error: let!(:unpublished_key) { FactoryGirl.create(:key) }
     ActiveRecord::RecordInvalid:
       Validation failed: Email has already been taken, Email has already been taken, Username has already been taken
     # ./spec/requests/key_pages_spec.rb:8:in `block (3 levels) in <top (required)>'

And for the chords:

1) Chord pages viewing chords on the pending chords page for an admin 
     Failure/Error: let!(:unpublished_chord) { FactoryGirl.create(:chord) }
     ActiveRecord::RecordInvalid:
       Validation failed: Email has already been taken, Email has already been taken, Username has already been taken
     # ./spec/requests/chord_pages_spec.rb:4:in `block (2 levels) in <top (required)>'

  2) Chord pages viewing chords on the pending chords page for an admin does not redirect to the sign up page
     Failure/Error: let!(:unpublished_chord) { FactoryGirl.create(:chord) }
     ActiveRecord::RecordInvalid:
       Validation failed: Email has already been taken, Email has already been taken, Username has already been taken
     # ./spec/requests/chord_pages_spec.rb:4:in `block (2 levels) in <top (required)>'

  3) Chord pages viewing chords on the pending chords page for an admin 
     Failure/Error: let!(:unpublished_chord) { FactoryGirl.create(:chord) }
     ActiveRecord::RecordInvalid:
       Validation failed: Email has already been taken, Email has already been taken, Username has already been taken
     # ./spec/requests/chord_pages_spec.rb:4:in `block (2 levels) in <top (required)>'

EDIT: Adding Spec

require 'spec_helper'

describe Key do
  let(:user) { FactoryGirl.create(:user) }
  let(:key)  { FactoryGirl.create(:key, user: user) }
  let(:chord)  { FactoryGirl.create(:chord, user: user) }
  let(:chord_2)  { FactoryGirl.create(:chord, user: user) }
  let(:c_chord) { FactoryGirl.create(:chord, user: user) }
  let(:note)  { FactoryGirl.create(:note) }
  let(:note_2) { FactoryGirl.create(:note) }
  let(:note_3) { FactoryGirl.create(:note) }
  let(:note_4) { FactoryGirl.create(:note) }

  subject { key }

  describe "its attributes" do
    it { should respond_to(:name) }

    describe "when name is blank" do
      before { key.name = nil }
      it { should_not be_valid }
    end

    it { should respond_to(:description) }

    describe "when description is blank" do
      before { key.description = nil }
      it { should_not be_valid }
    end

    it { should respond_to(:user_id) }
    it { should respond_to(:user) }
    its(:user) { should eq user }

    describe "when user_id is not present" do
      before { key.user_id = nil }
      it { should_not be_valid }
    end

    it { should be_valid }

    it { should respond_to(:keychords) }
    it { should respond_to(:chords) }

    it { should respond_to(:state) }
    it { should respond_to(:submit) }
    it { should respond_to(:withdraw) }
    it { should respond_to(:approve) }
    it { should respond_to(:unpublish) }

    it { should respond_to(:root_note) }
    it { should respond_to(:accidental) }
    it { should respond_to(:third) }

    describe "when root_note is blank" do
      before { key.root_note = nil }
      it { should_not be_valid }
    end

    describe "when root_note is not in root_noteS" do
      before { key.root_note = 'X' }
      it { should_not be_valid }
    end

    describe "when accidental is blank" do
      before { key.accidental = nil }
      it { should_not be_valid }
    end

    describe "when accidental is not in ACCIDENTALS" do
      before { key.accidental = 'Not an Accidental' }
      it { should_not be_valid }
    end

    describe "when third is blank" do
      before { key.third = nil }
      it { should_not be_valid }
    end

    describe "when third is not in THIRDS" do
      before { key.third = 'Not in Thirds' }
      it { should_not be_valid }
    end
  end

  describe "state changes" do
    it "should change from unpublished to submitted when submitted" do
    expect{ key.submit }.to change{key.state}.from('Unpublished').to('Submitted')
    end

    it "should change from submitted to unpublished when withdrawn" do
      key.state = 'Submitted'
      expect{ key.withdraw }.to change{key.state}.from('Submitted').to('Unpublished')
    end

    it "should change from submitted to approved when approved" do
      key.state = 'Submitted'
      expect{ key.approve }.to change{key.state}.from('Submitted').to('Published')
    end

    it "should change from submitted to unpublished when rejected" do
      key.state = 'Submitted'
      expect{ key.reject }.to change{key.state}.from('Submitted').to('Unpublished')
    end  

    it "should change from published to unpublished when unpublished" do
      key.state = 'Published'
      expect{ key.unpublish }.to change{key.state}.from('Published').to('Unpublished')
    end
  end  

  describe "its chords" do
    it "should not have any chords" do
      key.chords.count.should == 0
    end

    it "should not has_chord?" do
      expect(key.haschord?(chord)).to be_false
    end

    it "add chords should change number of chords" do
      expect { key.addchord!(chord) }.to change{key.chords.count}.from(0).to(1)
    end

    describe "after adding a chord" do
      before { key.addchord!(chord) }

      it "should add the chord" do
        expect(key.chords).to include(chord)
      end

      it "should only have the added chord" do
        expect(key.chords).not_to include(chord_2)
      end

      it "should haschord?(chord)" do
        expect(key.haschord?(chord)).to be_true
      end

      it "should not haschord?(chord2)" do
        expect(key.haschord?(chord_2)).to be_false
      end
    end
  end

  describe "its notes" do
    before do
      chord.notes << [note, note_2]
      chord_2.notes << [note_3, note_4]
    end

    describe "before adding chords" do
      it "should not have any notes" do
        key.notes.count.should == 0
      end

      it "should not hasnote?(note)" do
        expect(key.hasnote?(note)).to be_false
      end
    end

    describe "after adding a chord" do
      before { key.chords << chord }

      it "should have notes" do
        key.notes.count.should == 2
      end

      it "should have notes 1 and 2" do
        expect(key.notes).to include(note, note_2)
      end

      it "should not have notes 3 or 4" do
        expect(key.notes).not_to include(note_3, note_4)
      end

      it "should hasnote?(note)" do
        expect(key.hasnote?(note)).to be_true
      end
    end
  end
end
Uncut answered 28/2, 2014 at 19:14 Comment(2)
Show us your test file. Also do you have use_transaction_fixtures set to true in spec_helper.rb?Clancy
@Vimsha, I've added the spec that generates the first error (key_pages_spec.rb). use_transactional_fixtures = true in my spec_helper. ThanksUncut
A
11

You can use database_cleaner.

Add it to your Gemfile, and add the following to your spec_helper:

config.before(:suite) do
  DatabaseCleaner[:active_record].strategy = :transaction
  DatabaseCleaner.clean_with(:truncation)
end

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

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

This will make sure your data will be truncated after each spec.

Angle answered 28/2, 2014 at 20:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.