2010-11-12 15:58:25 -05:00
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
describe "a generated attributes hash" do
|
2011-01-19 19:47:49 -05:00
|
|
|
include FactoryGirl::Syntax::Methods
|
|
|
|
|
2010-11-12 15:58:25 -05:00
|
|
|
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
|
2011-06-29 14:43:15 -04:00
|
|
|
factory :user
|
2010-11-12 15:58:25 -05:00
|
|
|
|
|
|
|
factory :post do
|
|
|
|
title { "default title" }
|
|
|
|
body { "default body" }
|
|
|
|
summary { title }
|
|
|
|
user
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-01-19 19:47:49 -05:00
|
|
|
subject { attributes_for(:post, :title => 'overridden title') }
|
2010-11-12 15:58:25 -05:00
|
|
|
|
|
|
|
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
|
2011-12-05 21:00:46 -05:00
|
|
|
subject.should_not have_key(:user_id)
|
|
|
|
subject.should_not have_key(:user)
|
2010-11-12 15:58:25 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-10-12 11:35:10 -04:00
|
|
|
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
|
2011-11-22 18:01:01 -05:00
|
|
|
|
|
|
|
describe "`attributes_for` for a class whose constructor has required params" do
|
|
|
|
before do
|
|
|
|
define_model("User", :name => :string) do
|
|
|
|
def initialize(arg1, arg2); end
|
|
|
|
end
|
|
|
|
|
|
|
|
FactoryGirl.define do
|
|
|
|
factory :user do
|
|
|
|
name "John Doe"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
subject { FactoryGirl.attributes_for(:user) }
|
|
|
|
its([:name]) { should == "John Doe" }
|
|
|
|
end
|