Sha256: 10224b9f2a06c0a26fa1ed0f9553bfac5e92d25e895292e5b7065bc94d6b6a2b

Contents?: true

Size: 1.35 KB

Versions: 3

Compression:

Stored size: 1.35 KB

Contents

require 'spec_helper'

describe "a generated attributes hash" do
  include FactoryGirl::Syntax::Methods

  before do
    define_model('User')

    define_model('Post', :title   => :string,
                         :body    => :string,
                         :summary => :string,
                         :user_id => :integer) do
      belongs_to :user
    end

    FactoryGirl.define do
      factory :user

      factory :post do
        title { "default title" }
        body { "default body" }
        summary { title }
        user
      end
    end
  end

  subject { attributes_for(:post, :title => 'overridden title') }

  it "assigns an overridden value" do
    subject[:title].should == "overridden title"
  end

  it "assigns a default value" do
    subject[:body].should == "default body"
  end

  it "assigns a lazy, dependent attribute" do
    subject[:summary].should == "overridden title"
  end

  it "doesn't assign associations" do
    subject[:user_id].should be_nil
  end
end

describe "calling `attributes_for` with a block" do
  include FactoryGirl::Syntax::Methods

  before do
    define_model('Company', :name => :string)

    FactoryGirl.define do
      factory :company
    end
  end

  it "passes the hash of attributes" do
    attributes_for(:company, :name => 'thoughtbot') do |attributes|
      attributes[:name].should eq('thoughtbot')
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
factory_girl-2.3.1 spec/acceptance/attributes_for_spec.rb
factory_girl-2.3.0 spec/acceptance/attributes_for_spec.rb
factory_girl-2.2.0 spec/acceptance/attributes_for_spec.rb