Sha256: bce763f6e440ebaa9b0a678411e8572c2c8608587cd38aa5daf4868571a55438

Contents?: true

Size: 1.57 KB

Versions: 3

Compression:

Stored size: 1.57 KB

Contents

require 'spec_helper'

describe FactoryGirl::Attribute::Dynamic do
  let(:name)  { :first_name }
  let(:proxy) { stub("proxy", :set => nil) }
  let(:block) { lambda { } }

  subject { FactoryGirl::Attribute::Dynamic.new(name, block) }

  its(:name) { should == name }

  context "with a block returning a static value" do
    let(:block) { lambda { "value" } }

    it "calls the block to set a value" do
      subject.add_to(proxy)
      proxy.should have_received(:set).with(name, "value", false)
    end
  end

  context "with a block returning its block-level variable" do
    let(:block) { lambda {|thing| thing } }

    it "yields the proxy to the block" do
      subject.add_to(proxy)
      proxy.should have_received(:set).with(name, proxy, false)
    end
  end

  context "with a block referencing an attribute on the proxy" do
    let(:block)  { lambda { attribute_defined_on_proxy } }
    let(:result) { "other attribute value" }

    before do
      proxy.stubs(:attribute_defined_on_proxy => result)
    end

    it "evaluates the attribute from the proxy" do
      subject.add_to(proxy)
      proxy.should have_received(:set).with(name, result, false)
    end
  end

  context "with a block returning a sequence" do
    let(:block) { lambda { Factory.sequence(:email) } }

    it "raises a sequence abuse error" do
      expect { subject.add_to(proxy) }.to raise_error(FactoryGirl::SequenceAbuseError)
    end
  end
end

describe FactoryGirl::Attribute::Dynamic, "with a string name" do
  subject    { FactoryGirl::Attribute::Dynamic.new("name", nil) }
  its(:name) { should == :name }
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
factory_girl-2.1.2 spec/factory_girl/attribute/dynamic_spec.rb
factory_girl-2.1.0 spec/factory_girl/attribute/dynamic_spec.rb
factory_girl-2.0.5 spec/factory_girl/attribute/dynamic_spec.rb