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')
|
2012-04-17 08:38:12 -04:00
|
|
|
define_model('Comment')
|
2010-11-12 15:58:25 -05:00
|
|
|
|
2012-03-09 17:20:38 -05:00
|
|
|
define_model('Post', title: :string,
|
|
|
|
body: :string,
|
|
|
|
summary: :string,
|
|
|
|
user_id: :integer) do
|
2010-11-12 15:58:25 -05:00
|
|
|
belongs_to :user
|
2012-04-17 08:38:12 -04:00
|
|
|
has_many :comments
|
2010-11-12 15:58:25 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
FactoryGirl.define do
|
2011-06-29 14:43:15 -04:00
|
|
|
factory :user
|
2012-04-17 08:38:12 -04:00
|
|
|
factory :comment
|
2010-11-12 15:58:25 -05:00
|
|
|
|
|
|
|
factory :post do
|
|
|
|
title { "default title" }
|
|
|
|
body { "default body" }
|
|
|
|
summary { title }
|
|
|
|
user
|
2012-04-17 08:38:12 -04:00
|
|
|
comments do |c|
|
|
|
|
[c.association(:comment)]
|
|
|
|
end
|
2010-11-12 15:58:25 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-03-09 17:20:38 -05:00
|
|
|
subject { attributes_for(:post, title: 'overridden title') }
|
2010-11-12 15:58:25 -05:00
|
|
|
|
|
|
|
it "assigns an overridden value" do
|
2013-01-18 13:27:57 -05:00
|
|
|
expect(subject[:title]).to eq "overridden title"
|
2010-11-12 15:58:25 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "assigns a default value" do
|
2013-01-18 13:27:57 -05:00
|
|
|
expect(subject[:body]).to eq "default body"
|
2010-11-12 15:58:25 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "assigns a lazy, dependent attribute" do
|
2013-01-18 13:27:57 -05:00
|
|
|
expect(subject[:summary]).to eq "overridden title"
|
2010-11-12 15:58:25 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't assign associations" do
|
2013-01-18 13:27:57 -05:00
|
|
|
expect(subject).not_to have_key(:user_id)
|
|
|
|
expect(subject).not_to 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
|
2012-03-09 17:20:38 -05:00
|
|
|
define_model('Company', name: :string)
|
2011-10-12 11:35:10 -04:00
|
|
|
|
|
|
|
FactoryGirl.define do
|
|
|
|
factory :company
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it "passes the hash of attributes" do
|
2012-03-09 17:20:38 -05:00
|
|
|
attributes_for(:company, name: 'thoughtbot') do |attributes|
|
2013-01-18 13:27:57 -05:00
|
|
|
expect(attributes[:name]).to eq('thoughtbot')
|
2011-10-12 11:35:10 -04:00
|
|
|
end
|
|
|
|
end
|
2011-12-20 13:10:52 -05:00
|
|
|
|
|
|
|
it "returns the hash of attributes" do
|
|
|
|
expected = nil
|
2013-01-18 13:27:57 -05:00
|
|
|
|
|
|
|
result = attributes_for(:company) do |attributes|
|
2011-12-20 13:10:52 -05:00
|
|
|
expected = attributes
|
|
|
|
"hello!"
|
2013-01-18 13:27:57 -05:00
|
|
|
end
|
|
|
|
expect(result).to eq expected
|
2011-12-20 13:10:52 -05:00
|
|
|
end
|
2011-10-12 11:35:10 -04:00
|
|
|
end
|
2011-11-22 18:01:01 -05:00
|
|
|
|
|
|
|
describe "`attributes_for` for a class whose constructor has required params" do
|
|
|
|
before do
|
2012-03-09 17:20:38 -05:00
|
|
|
define_model("User", name: :string) do
|
2011-11-22 18:01:01 -05:00
|
|
|
def initialize(arg1, arg2); end
|
|
|
|
end
|
|
|
|
|
|
|
|
FactoryGirl.define do
|
|
|
|
factory :user do
|
|
|
|
name "John Doe"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
subject { FactoryGirl.attributes_for(:user) }
|
2013-01-18 13:27:57 -05:00
|
|
|
its([:name]) { should eq "John Doe" }
|
2011-11-22 18:01:01 -05:00
|
|
|
end
|