2008-05-31 20:12:57 -04:00
|
|
|
require(File.join(File.dirname(__FILE__), 'test_helper'))
|
|
|
|
|
|
|
|
class IntegrationTest < Test::Unit::TestCase
|
|
|
|
|
|
|
|
def setup
|
|
|
|
Factory.define :user do |f|
|
|
|
|
f.first_name 'Jimi'
|
|
|
|
f.last_name 'Hendrix'
|
|
|
|
f.admin false
|
|
|
|
f.email {|a| "#{a.first_name}.#{a.last_name}@example.com".downcase }
|
|
|
|
end
|
|
|
|
|
|
|
|
Factory.define :post do |f|
|
|
|
|
f.title 'Test Post'
|
|
|
|
f.author {|a| a.association(:user) }
|
|
|
|
end
|
|
|
|
|
|
|
|
Factory.define :admin, :class => User do |f|
|
|
|
|
f.first_name 'Ben'
|
|
|
|
f.last_name 'Stein'
|
|
|
|
f.admin true
|
|
|
|
f.email {|a| "#{a.first_name}.#{a.last_name}@example.com".downcase }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def teardown
|
|
|
|
Factory.send(:class_variable_get, "@@factories").clear
|
|
|
|
end
|
|
|
|
|
|
|
|
context "a generated attributes hash" do
|
|
|
|
|
|
|
|
setup do
|
|
|
|
@attrs = Factory.attributes_for(:user, :first_name => 'Bill')
|
|
|
|
end
|
|
|
|
|
|
|
|
should "assign all attributes" do
|
|
|
|
assert_equal [:admin, :email, :first_name, :last_name],
|
|
|
|
@attrs.keys.sort {|a, b| a.to_s <=> b.to_s }
|
|
|
|
end
|
|
|
|
|
|
|
|
should "correctly assign lazy, dependent attributes" do
|
|
|
|
assert_equal "bill.hendrix@example.com", @attrs[:email]
|
|
|
|
end
|
|
|
|
|
|
|
|
should "override attrbutes" do
|
|
|
|
assert_equal 'Bill', @attrs[:first_name]
|
|
|
|
end
|
|
|
|
|
2008-06-01 01:54:19 -04:00
|
|
|
should "not assign associations" do
|
|
|
|
assert_nil Factory.attributes_for(:post)[:author]
|
|
|
|
end
|
|
|
|
|
2008-05-31 20:12:57 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
context "a built instance" do
|
|
|
|
|
|
|
|
setup do
|
|
|
|
@instance = Factory.build(:post)
|
|
|
|
end
|
|
|
|
|
|
|
|
should "not be saved" do
|
|
|
|
assert @instance.new_record?
|
|
|
|
end
|
|
|
|
|
|
|
|
should "assign associations" do
|
|
|
|
assert_kind_of User, @instance.author
|
|
|
|
end
|
|
|
|
|
|
|
|
should "not save associations" do
|
|
|
|
assert @instance.author.new_record?
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2008-06-01 01:54:19 -04:00
|
|
|
context "a created instance" do
|
2008-05-31 20:12:57 -04:00
|
|
|
|
|
|
|
setup do
|
|
|
|
@instance = Factory.create(:post)
|
|
|
|
end
|
|
|
|
|
|
|
|
should "be saved" do
|
|
|
|
assert !@instance.new_record?
|
|
|
|
end
|
|
|
|
|
|
|
|
should "assign associations" do
|
|
|
|
assert_kind_of User, @instance.author
|
|
|
|
end
|
|
|
|
|
|
|
|
should "save associations" do
|
|
|
|
assert !@instance.author.new_record?
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
context "an instance generated by a factory with a custom class name" do
|
|
|
|
|
|
|
|
setup do
|
|
|
|
@instance = Factory.create(:admin)
|
|
|
|
end
|
|
|
|
|
|
|
|
should "use the correct class name" do
|
|
|
|
assert_kind_of User, @instance
|
|
|
|
end
|
|
|
|
|
|
|
|
should "use the correct factory definition" do
|
|
|
|
assert @instance.admin?
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|