Sha256: 42839426da358c6189ccf6ffc62a0b6b92d8ec3c34b41174d99e1a51bfb217d3

Contents?: true

Size: 1.42 KB

Versions: 35

Compression:

Stored size: 1.42 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
      FactoryGirl.build(:user).age_copy.should == 18
    end

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

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

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

    it "uses the overridden value during attributes_for" do
      FactoryGirl.attributes_for(:user, :age_copy => 25)[:age_copy].should == 25
    end
  end

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

    it "uses the overridden value during attributes_for" do
      FactoryGirl.attributes_for(:user, :age => 25)[:age_copy].should == 25
    end
  end
end

Version data entries

35 entries across 30 versions & 3 rubygems

Version Path
judge-2.0.5 vendor/bundle/ruby/2.1.0/gems/factory_girl-2.6.4/spec/acceptance/attributes_from_instance_spec.rb
challah-0.6.2 vendor/bundle/gems/factory_girl-2.6.1/spec/acceptance/attributes_from_instance_spec.rb
challah-0.6.1 vendor/bundle/gems/factory_girl-2.6.1/spec/acceptance/attributes_from_instance_spec.rb
challah-0.6.1 vendor/bundle/gems/factory_girl-2.5.1/spec/acceptance/attributes_from_instance_spec.rb
challah-0.6.0 vendor/bundle/gems/factory_girl-2.5.1/spec/acceptance/attributes_from_instance_spec.rb
challah-0.6.0 vendor/bundle/gems/factory_girl-2.6.1/spec/acceptance/attributes_from_instance_spec.rb
challah-0.5.4 vendor/bundle/gems/factory_girl-2.5.1/spec/acceptance/attributes_from_instance_spec.rb
challah-0.5.4 vendor/bundle/gems/factory_girl-2.6.1/spec/acceptance/attributes_from_instance_spec.rb
challah-0.5.3 vendor/bundle/gems/factory_girl-2.6.1/spec/acceptance/attributes_from_instance_spec.rb
challah-0.5.3 vendor/bundle/gems/factory_girl-2.5.1/spec/acceptance/attributes_from_instance_spec.rb
challah-0.5.2 vendor/bundle/gems/factory_girl-2.5.1/spec/acceptance/attributes_from_instance_spec.rb
challah-0.5.2 vendor/bundle/gems/factory_girl-2.6.1/spec/acceptance/attributes_from_instance_spec.rb
factory_girl-2.6.4 spec/acceptance/attributes_from_instance_spec.rb
factory_girl-2.6.3 spec/acceptance/attributes_from_instance_spec.rb
factory_girl-2.6.2 spec/acceptance/attributes_from_instance_spec.rb
challah-0.5.1 vendor/bundle/gems/factory_girl-2.6.1/spec/acceptance/attributes_from_instance_spec.rb
factory_girl-2.6.1 spec/acceptance/attributes_from_instance_spec.rb
challah-0.5.0 vendor/bundle/gems/factory_girl-2.5.1/spec/acceptance/attributes_from_instance_spec.rb
factory_girl-2.6.0 spec/acceptance/attributes_from_instance_spec.rb
factory_girl-2.5.2 spec/acceptance/attributes_from_instance_spec.rb