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/test/attribute_proxy_test.rb

101 lines
2.4 KiB
Ruby

require(File.join(File.dirname(__FILE__), 'test_helper'))
class AttributeProxyTest < Test::Unit::TestCase
context "an association proxy" do
setup do
@factory = mock('factory')
@attr = :user
@attrs = { :first_name => 'John' }
@strategy = :create
@proxy = Factory::AttributeProxy.new(@factory, @attr, @strategy, @attrs)
end
should "have a factory" do
assert_equal @factory, @proxy.factory
end
should "have an attribute name" do
assert_equal @attr, @proxy.attribute_name
end
should "have a build strategy" do
assert_equal @strategy, @proxy.strategy
end
should "have attributes" do
assert_equal @attrs, @proxy.current_values
end
context "building an association" do
setup do
@association = mock('built-user')
@name = :user
@attribs = { :first_name => 'Billy' }
Factory.stubs(@strategy).returns(@association)
end
should "delegate to the appropriate method on Factory" do
Factory.expects(@strategy).with(@name, @attribs).returns(@association)
@proxy.association(@name, @attribs)
end
should "return the built association" do
assert_equal @association, @proxy.association(@name)
end
end
context "building an association using the attributes_for strategy" do
setup do
@strategy = :attributes_for
@proxy = Factory::AttributeProxy.new(@factory, @attr, @strategy, @attrs)
end
should "not build the association" do
Factory.expects(@strategy).never
@proxy.association(:user)
end
should "return nil for the association" do
Factory.stubs(@strategy).returns(:user)
assert_nil @proxy.association(:user)
end
end
context "fetching the value of an attribute" do
setup do
@attr = :first_name
end
should "return the correct value" do
assert_equal @attrs[@attr], @proxy.value_for(@attr)
end
should "call value_for for undefined methods" do
assert_equal @attrs[@attr], @proxy.send(@attr)
end
end
context "fetching the value of an undefined attribute" do
setup do
@attr = :beachball
end
should "raise an ArgumentError" do
assert_raise(ArgumentError) { @proxy.value_for(@attr) }
end
end
end
end