1
0
Fork 0
mirror of https://github.com/thoughtbot/factory_bot.git synced 2022-11-09 11:43:51 -05:00
thoughtbot--factory_bot/spec/acceptance/attributes_for_spec.rb

98 lines
2.1 KiB
Ruby
Raw Normal View History

require 'spec_helper'
describe "a generated attributes hash" do
include FactoryGirl::Syntax::Methods
before do
define_model('User')
2012-04-17 08:38:12 -04:00
define_model('Comment')
2012-03-09 17:20:38 -05:00
define_model('Post', title: :string,
body: :string,
summary: :string,
user_id: :integer) do
belongs_to :user
2012-04-17 08:38:12 -04:00
has_many :comments
end
FactoryGirl.define do
factory :user
2012-04-17 08:38:12 -04:00
factory :comment
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
end
end
end
2012-03-09 17:20:38 -05:00
subject { attributes_for(:post, title: 'overridden title') }
it "assigns an overridden value" do
2013-01-18 13:27:57 -05:00
expect(subject[:title]).to eq "overridden title"
end
it "assigns a default value" do
2013-01-18 13:27:57 -05:00
expect(subject[:body]).to eq "default body"
end
it "assigns a lazy, dependent attribute" do
2013-01-18 13:27:57 -05:00
expect(subject[:summary]).to eq "overridden title"
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)
end
end
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)
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')
end
end
it "returns the hash of attributes" do
expected = nil
2013-01-18 13:27:57 -05:00
result = attributes_for(:company) do |attributes|
expected = attributes
"hello!"
2013-01-18 13:27:57 -05:00
end
expect(result).to eq expected
end
end
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
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" }
end