Sha256: ab41a1c2549fc759f629eb8f6046fce7801165a3fb02c06d425eb10b485b337b

Contents?: true

Size: 1.43 KB

Versions: 16

Compression:

Stored size: 1.43 KB

Contents

require "spec_helper"

describe "calling methods on the model instance" do
  before do
    define_model('User', age: :integer, age_copy: :integer) do
      def age
        read_attribute(:age) || 18
      end
    end

    FactoryGirl.define do
      factory :user do
        age_copy { age }
      end
    end
  end

  context "without the attribute being overridden" do
    it "returns the correct value from the instance" do
      expect(FactoryGirl.build(:user).age_copy).to eq 18
    end

    it "returns nil during attributes_for" do
      expect(FactoryGirl.attributes_for(:user)[:age_copy]).to be_nil
    end

    it "doesn't instantiate a record with attributes_for" do
      User.stubs(:new)
      FactoryGirl.attributes_for(:user)
      expect(User).to have_received(:new).never
    end
  end

  context "with the attribute being overridden" do
    it "uses the overridden value" do
      expect(FactoryGirl.build(:user, age_copy: nil).age_copy).to be_nil
    end

    it "uses the overridden value during attributes_for" do
      expect(FactoryGirl.attributes_for(:user, age_copy: 25)[:age_copy]).to eq 25
    end
  end

  context "with the referenced attribute being overridden" do
    it "uses the overridden value" do
      expect(FactoryGirl.build(:user, age: nil).age_copy).to be_nil
    end

    it "uses the overridden value during attributes_for" do
      expect(FactoryGirl.attributes_for(:user, age: 25)[:age_copy]).to eq 25
    end
  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/attributes_from_instance_spec.rb
solidus_backend-1.0.0.pre3 vendor/bundle/gems/factory_girl-4.5.0/spec/acceptance/attributes_from_instance_spec.rb
solidus_backend-1.0.0.pre2 vendor/bundle/gems/factory_girl-4.5.0/spec/acceptance/attributes_from_instance_spec.rb
solidus_backend-1.0.0.pre vendor/bundle/gems/factory_girl-4.5.0/spec/acceptance/attributes_from_instance_spec.rb
apl-library-0.0.90 vendor/bundle/ruby/1.9.1/gems/factory_girl-4.4.0/spec/acceptance/attributes_from_instance_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/attributes_from_instance_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/attributes_from_instance_spec.rb
apl-library-0.0.90 vendor/bundle/ruby/2.1.0/gems/factory_girl-4.4.0/spec/acceptance/attributes_from_instance_spec.rb
factory_girl-4.5.0 spec/acceptance/attributes_from_instance_spec.rb
factory_girl-4.4.0 spec/acceptance/attributes_from_instance_spec.rb
factory_girl-4.3.0 spec/acceptance/attributes_from_instance_spec.rb
challah-1.0.0 vendor/bundle/gems/factory_girl-4.2.0/spec/acceptance/attributes_from_instance_spec.rb
challah-1.0.0.beta3 vendor/bundle/gems/factory_girl-4.2.0/spec/acceptance/attributes_from_instance_spec.rb
challah-1.0.0.beta2 vendor/bundle/gems/factory_girl-4.2.0/spec/acceptance/attributes_from_instance_spec.rb
challah-1.0.0.beta vendor/bundle/gems/factory_girl-4.2.0/spec/acceptance/attributes_from_instance_spec.rb
factory_girl-4.2.0 spec/acceptance/attributes_from_instance_spec.rb