Search code examples
ruby-on-railstestingrspecfactory-botgrape-api

Rspec with transactional_fixtures=true throws uniqueness errors


I'm testing my API, build with Grape and Rails, with RSpec and get a lot of uniqueness validation errors.

I'm using config.use_transactional_fixtures = true to rollback the database after each test, but I'm getting this errors:

  7) API GET /api/v1/quotations returns 200 when there is at least one quotation
     Failure/Error: FactoryGirl.create(:quotation)
     ActiveRecord::RecordInvalid:
       Validation failed: Number has already been taken
     # ./spec/requests/api/quotations_spec.rb:56:in `block (3 levels) in <top (required)>'

I think the problem is, that my models have transitive dependencies:

Clients have Contacts and Contacts have Quotations. So when creating a quotation, a corresponding contact with a corresponding client should be created. And a quotation always belongs to a Request. But it seems, that FactoryGirl doesn't create new instances?

The validations error does not belong to a quotation. If I remove the uniqueness validation on the quotation model, I get the same error. Actually, it belongs to the client model.

It seems, that not all tables get truncated? I event tried out DatabaseCleaner gem, but get the same errors (disabled use_transactional_fixtures before tried DatabaseCleaner).

Note: I'm using Postgres and the apartment gem for multi-tenancy.

That are my factories:

Client factory

FactoryGirl.define do

  factory :client do
    number Faker::Number.number(8)
    company Faker::Company.name
    address1 Faker::Address.street_address
    address2 Faker::Address.secondary_address
    city Faker::Address.city
    zip Faker::Address.zip
    country Faker::Address.country
    tax '19'
    email Faker::Internet.email
    phone Faker::PhoneNumber.phone_number
    web Faker::Internet.url
  end

end

Contact factory

FactoryGirl.define do

  factory :contact do
    title Faker::Name.title
    name Faker::Name.name
    surname Faker::Name.last_name
    department Faker::Commerce.department
    email Faker::Internet.email
    phone Faker::PhoneNumber.phone_number
    password 'secret'
    password_confirmation 'secret'
    client
  end

end

Quotation factory

FactoryGirl.define do

  factory :quotation do
    number Faker::Number.number(8)
    title Faker::Lorem.word
    payable Faker::Lorem.sentence
    request
    contact
  end

end

Request factory

FactoryGirl.define do

  factory :request do
    number Faker::Number.number(8)
    title Faker::Lorem.word
    content Faker::Lorem.sentence
    contact
  end

end

Part of my quotations_spec.rb

require 'spec_helper'

describe API do

  include Rack::Test::Methods

  def app
    API
  end

  ####################################################################################################################
  # Authentication
  ####################################################################################################################

  let(:url) { 'http://testing.domain.com' }
  let!(:access_token) do
    user = FactoryGirl.create(:user)
    api_key = FactoryGirl.create(:api_key_session, foreign_id: user.id)
    api_key.access_token
  end

  describe 'GET /api/v1/quotations' do

    it 'returns 401 when unauthorized' do

      get "#{url}/api/v1/quotations"
      expect(last_response.status).to eq 401

    end

  end

  ####################################################################################################################
  # GET quotations
  ####################################################################################################################

  describe 'GET /api/v1/quotations' do

    #----------------------------------------------------------------------------------------------------------------#


    it 'returns 404 when quotations not found' do

      header 'X-Access-Key', access_token
      get "#{url}/api/v1/quotations"

      expect(last_response.status).to eq 404

    end

    #----------------------------------------------------------------------------------------------------------------#

    it 'returns 200 when there is at least one quotation' do

      FactoryGirl.create(:quotation)

      header 'X-Access-Key', access_token
      get "#{url}/api/v1/quotations"

      expect(last_response.status).to eq 200

    end

    #----------------------------------------------------------------------------------------------------------------#

  end

end

Update 1

I switched to DatabaseCleaner and tried to debug this:

config.after(:each) do

    puts
    puts '####'
    puts Client.all
    puts '####'
    puts Contact.all
    puts '####'
    puts

    DatabaseCleaner.clean

    puts
    puts '####'
    puts Client.all
    puts '####'
    puts Contact.all
    puts '####'
    puts

    # Reset tentant back to `public`
    Apartment::Database.reset

  end

Actually, the database gets cleaned, so Contact.all and Client.all is empty after Database.clean. This is confusing, because otherwise the validation errors shouldn't occur?


Solution

  • I figured it out. Thanks to this answer: https://stackoverflow.com/a/16726614/1184904

    I have to replace the unique fields with sequences like this:

    FactoryGirl.define do
    
      factory :user do
        name Faker::Name.name
        surname Faker::Name.last_name
        sequence :email do |n|
          "foo#{n}@bar.de"
        end
        password 'secret'
        password_confirmation 'secret'
      end
    
    end