Sha256: b17f54a1b3223d4023c00a1a80a4ad8addf2387152551bfdff53c42c278925d6

Contents?: true

Size: 1.46 KB

Versions: 54

Compression:

Stored size: 1.46 KB

Contents

require "spec_helper"

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

    time_to_execute.should be_within(0.01).of(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

    tracked_invocations[:slow_user].should == { build: 2, attributes_for: 1 }
    tracked_invocations[:user].should == { build: 5, create: 2 }
  end
end

Version data entries

54 entries across 54 versions & 4 rubygems

Version Path
classiccms-0.5.13 vendor/bundle/gems/factory_girl-3.2.0/spec/acceptance/activesupport_instrumentation_spec.rb
classiccms-0.5.12 vendor/bundle/gems/factory_girl-3.2.0/spec/acceptance/activesupport_instrumentation_spec.rb
classiccms-0.5.11 vendor/bundle/gems/factory_girl-3.2.0/spec/acceptance/activesupport_instrumentation_spec.rb
classiccms-0.5.10 vendor/bundle/gems/factory_girl-3.2.0/spec/acceptance/activesupport_instrumentation_spec.rb
classiccms-0.5.9 vendor/bundle/gems/factory_girl-3.2.0/spec/acceptance/activesupport_instrumentation_spec.rb
classiccms-0.5.8 vendor/bundle/gems/factory_girl-3.2.0/spec/acceptance/activesupport_instrumentation_spec.rb
classiccms-0.5.7 vendor/bundle/gems/factory_girl-3.2.0/spec/acceptance/activesupport_instrumentation_spec.rb
classiccms-0.5.6 vendor/bundle/gems/factory_girl-3.2.0/spec/acceptance/activesupport_instrumentation_spec.rb
classiccms-0.5.5 vendor/bundle/gems/factory_girl-3.2.0/spec/acceptance/activesupport_instrumentation_spec.rb
classiccms-0.5.2 vendor/bundle/gems/factory_girl-3.2.0/spec/acceptance/activesupport_instrumentation_spec.rb
classiccms-0.5.1 vendor/bundle/gems/factory_girl-3.2.0/spec/acceptance/activesupport_instrumentation_spec.rb
classiccms-0.5.0 vendor/bundle/gems/factory_girl-3.2.0/spec/acceptance/activesupport_instrumentation_spec.rb
classiccms-0.4.2 vendor/bundle/gems/factory_girl-3.2.0/spec/acceptance/activesupport_instrumentation_spec.rb
classiccms-0.4.1 vendor/bundle/gems/factory_girl-3.2.0/spec/acceptance/activesupport_instrumentation_spec.rb
classiccms-0.4.0 vendor/bundle/gems/factory_girl-3.2.0/spec/acceptance/activesupport_instrumentation_spec.rb
classiccms-0.3.9 vendor/bundle/gems/factory_girl-3.2.0/spec/acceptance/activesupport_instrumentation_spec.rb
sunrise-cms-0.3.3 vendor/bundle/ruby/1.9.1/gems/rails-uploader-0.0.1/vendor/bundle/ruby/1.9.1/gems/factory_girl-3.2.0/spec/acceptance/activesupport_instrumentation_spec.rb
sunrise-cms-0.3.2 vendor/bundle/ruby/1.9.1/gems/rails-uploader-0.0.1/vendor/bundle/ruby/1.9.1/gems/factory_girl-3.2.0/spec/acceptance/activesupport_instrumentation_spec.rb
sunrise-cms-0.3.1 vendor/bundle/ruby/1.9.1/gems/rails-uploader-0.0.1/vendor/bundle/ruby/1.9.1/gems/factory_girl-3.2.0/spec/acceptance/activesupport_instrumentation_spec.rb
sunrise-cms-0.3.0 vendor/bundle/ruby/1.9.1/gems/rails-uploader-0.0.1/vendor/bundle/ruby/1.9.1/gems/factory_girl-3.2.0/spec/acceptance/activesupport_instrumentation_spec.rb