Sha256: 579d166a4ebd4d7e7691847a5975529fa2dc0407483e6b85b40df739fd3b9ad6

Contents?: true

Size: 1.9 KB

Versions: 13

Compression:

Stored size: 1.9 KB

Contents

require File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'spec_helper'))

describe Factory::Proxy::Build do
  before do
    @class       = Class.new
    @instance    = "built-instance"
    @association = "associated-instance"

    stub(@class).new { @instance }
    stub(@instance).attribute { 'value' }
    stub(Factory).create { @association }
    stub(@instance, :attribute=)
    stub(@instance, :owner=)

    @proxy = Factory::Proxy::Build.new(@class)
  end

  it "should instantiate the class" do
    @class.should have_received.new
  end

  describe "when asked to associate with another factory" do
    before do
      @proxy.associate(:owner, :user, {})
    end

    it "should create the associated instance" do
      Factory.should have_received.create(:user, {})
    end

    it "should set the associated instance" do
      @instance.should have_received.method_missing(:owner=, @association)
    end
  end

  it "should call Factory.create when building an association" do
    association = 'association'
    attribs     = { :first_name => 'Billy' }
    stub(Factory).create { association }
    @proxy.association(:user, attribs).should == association
    Factory.should have_received.create(:user, attribs)
  end

  it "should return the built instance when asked for the result" do
    @proxy.result.should == @instance
  end

  describe "when setting an attribute" do
    before do
      stub(@instance).attribute = 'value'
      @proxy.set(:attribute, 'value')
    end

    it "should set that value" do
      @instance.should have_received.method_missing(:attribute=, 'value')
    end
  end

  describe "when getting an attribute" do
    before do
      @result = @proxy.get(:attribute)
    end

    it "should ask the built class for the value" do
      @instance.should have_received.attribute
    end

    it "should return the value for that attribute" do
      @result.should == 'value'
    end
  end
end

Version data entries

13 entries across 13 versions & 8 rubygems

Version Path
BrettRasmussen-factory_girl-1.2.2 spec/factory_girl/proxy/build_spec.rb
BrettRasmussen-factory_girl-1.2.3 spec/factory_girl/proxy/build_spec.rb
agibralter-factory_girl-1.2.1 spec/factory_girl/proxy/build_spec.rb
lacomartincik-factory_girl-1.2.1.1 spec/factory_girl/proxy/build_spec.rb
qrush-factory_girl-1.2.1.1 spec/factory_girl/proxy/build_spec.rb
thoughtbot-factory_girl-1.2.2 spec/factory_girl/proxy/build_spec.rb
ttilley-factory_girl-1.2.2.1 spec/factory_girl/proxy/build_spec.rb
ttilley-factory_girl-1.2.2.2 spec/factory_girl/proxy/build_spec.rb
ttilley-factory_girl-1.2.2 spec/factory_girl/proxy/build_spec.rb
freegenie-factory_girl-1.2.4 spec/factory_girl/proxy/build_spec.rb
freegenie-factory_girl-1.2.3 spec/factory_girl/proxy/build_spec.rb
freegenie-factory_girl-1.2.2 spec/factory_girl/proxy/build_spec.rb
factory_girl-1.2.2 spec/factory_girl/proxy/build_spec.rb