Sha256: 54cce8643bba740f6e34fa93d3601e44417f178b1c8df85d0bffa2bb73832d31

Contents?: true

Size: 1.76 KB

Versions: 16

Compression:

Stored size: 1.76 KB

Contents

require "spec_helper"

unless ActiveSupport::Notifications.respond_to?(:subscribed)
  module SubscribedBehavior
    def subscribed(callback, *args, &block)
      subscriber = subscribe(*args, &callback)
      yield
    ensure
      unsubscribe(subscriber)
    end
  end

  ActiveSupport::Notifications.extend SubscribedBehavior
end

describe "using ActiveSupport::Instrumentation to track factory interaction" do
  before do
    define_model("User", email: :string)
    FactoryGirl.define do
      factory :user do
        email "john@example.com"

        factory :slow_user do
          after(:build) { Kernel.sleep(0.1) }
        end
      end

    end
  end

  it "tracks proper time of creating the record" do
    time_to_execute = 0
    callback = ->(name, start, finish, id, payload) { time_to_execute = finish - start }
    ActiveSupport::Notifications.subscribed(callback, "factory_girl.run_factory") do
      FactoryGirl.build(:slow_user)
    end

    expect(time_to_execute).to be >= 0.1
  end

  it "builds the correct payload" do
    tracked_invocations = {}

    callback = ->(name, start, finish, id, payload) do
      factory_name = payload[:name]
      strategy_name = payload[:strategy]
      tracked_invocations[factory_name] ||= {}
      tracked_invocations[factory_name][strategy_name] ||= 0
      tracked_invocations[factory_name][strategy_name] += 1
    end

    ActiveSupport::Notifications.subscribed(callback, "factory_girl.run_factory") do
      FactoryGirl.build_list(:slow_user, 2)
      FactoryGirl.build_list(:user, 5)
      FactoryGirl.create_list(:user, 2)
      FactoryGirl.attributes_for(:slow_user)
    end

    expect(tracked_invocations[:slow_user]).to eq(build: 2, attributes_for: 1)
    expect(tracked_invocations[:user]).to eq(build: 5, create: 2)
  end
end

Version data entries

16 entries across 13 versions & 5 rubygems

Version Path
sc_core-0.0.7 test/dummy/vendor/bundle/ruby/2.2.0/gems/factory_girl-4.5.0/spec/acceptance/activesupport_instrumentation_spec.rb
solidus_backend-1.0.0.pre3 vendor/bundle/gems/factory_girl-4.5.0/spec/acceptance/activesupport_instrumentation_spec.rb
solidus_backend-1.0.0.pre2 vendor/bundle/gems/factory_girl-4.5.0/spec/acceptance/activesupport_instrumentation_spec.rb
solidus_backend-1.0.0.pre vendor/bundle/gems/factory_girl-4.5.0/spec/acceptance/activesupport_instrumentation_spec.rb
apl-library-0.0.90 vendor/bundle/ruby/1.9.1/gems/factory_girl-4.4.0/spec/acceptance/activesupport_instrumentation_spec.rb
apl-library-0.0.90 vendor/bundle/ruby/2.1.0/gems/apl-library-0.0.90/vendor/bundle/ruby/1.9.1/gems/factory_girl-4.4.0/spec/acceptance/activesupport_instrumentation_spec.rb
apl-library-0.0.90 vendor/bundle/ruby/2.1.0/gems/apl-library-0.0.90/vendor/bundle/ruby/2.1.0/gems/factory_girl-4.4.0/spec/acceptance/activesupport_instrumentation_spec.rb
apl-library-0.0.90 vendor/bundle/ruby/2.1.0/gems/factory_girl-4.4.0/spec/acceptance/activesupport_instrumentation_spec.rb
factory_girl-4.5.0 spec/acceptance/activesupport_instrumentation_spec.rb
factory_girl-4.4.0 spec/acceptance/activesupport_instrumentation_spec.rb
factory_girl-4.3.0 spec/acceptance/activesupport_instrumentation_spec.rb
challah-1.0.0 vendor/bundle/gems/factory_girl-4.2.0/spec/acceptance/activesupport_instrumentation_spec.rb
challah-1.0.0.beta3 vendor/bundle/gems/factory_girl-4.2.0/spec/acceptance/activesupport_instrumentation_spec.rb
challah-1.0.0.beta2 vendor/bundle/gems/factory_girl-4.2.0/spec/acceptance/activesupport_instrumentation_spec.rb
challah-1.0.0.beta vendor/bundle/gems/factory_girl-4.2.0/spec/acceptance/activesupport_instrumentation_spec.rb
factory_girl-4.2.0 spec/acceptance/activesupport_instrumentation_spec.rb