具有transactional_fixtures = true的Rspec会引发唯一性错误

时间:2014-06-23 08:26:29

标签: ruby-on-rails testing rspec factory-bot grape-api

我正在使用RSpec测试我的API,使用Grape和Rails构建,并获得许多唯一性验证错误。

我在每次测试后使用config.use_transactional_fixtures = true回滚数据库,但我收到了这个错误:

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

我认为问题是,我的模型具有传递依赖性:

ClientsContactsContacts有引文。 因此,在创建报价单时,应创建与相应客户端的对应联系人。报价始终属于Request。但似乎FactoryGirl没有创建新实例?

验证错误属于报价单。如果我删除报价模型上的唯一性验证,我会得到相同的错误。实际上,它属于客户端模型。

似乎并非所有表都被截断了?我的事件尝试了DatabaseCleaner gem,但是在尝试使用DatabaseCleaner之前得到了相同的错误(禁用use_transactional_fixtures。)

注意:我使用Postgres和公寓宝石进行多租户。

那是我的工厂:

客户工厂

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

联系工厂

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

报价工厂

FactoryGirl.define do

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

end

申请工厂

FactoryGirl.define do

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

end

我的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

更新1

我切换到DatabaseCleaner并试图调试它:

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

实际上,数据库会被清除,因此Contact.allClient.allDatabase.clean为空。这很令人困惑,因为否则验证错误不会发生?

1 个答案:

答案 0 :(得分:0)

我明白了。感谢这个答案:https://stackoverflow.com/a/16726614/1184904

我必须用以下序列替换唯一字段:

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