Search code examples
ruby-on-railsrubyrspecfactory-botrspec-rails

Data persisting with RSpec-rails in a before(:each)


I have transactions set up in my rails_helper...

config.use_transactional_fixtures = true

My before each blocks look like this:

before(:each) do
  allow(subject).to receive_messages(
    :authenticate => true,
    :authorize => true
  )

@user_song = FactoryGirl.create(:user_song)
FactoryGirl.create(:user_playlist, user_id: @user_song.user_id, 
  album_id: @user_song.song.album_id)

allow_any_instance_of(ApplicationController).to receive_messages(
  :current_user => @user_song.user
)

Tests fail with validation errors, but, if I insert a Model.destroy_all for each used model before FactoryGirl's creation, it works. I'm confused.

Why? The Rspec rails guide says that it should be rolled-back? Is it something with FactoryGirl?


Solution

  • config.use_transactional_fixtures = true
    

    is for Rails builtin fixeture cleanup. It wound work with factory_girl. You should disable that and, install a db cleaner gem like this one: https://github.com/DatabaseCleaner/database_cleaner