提交 6e3c0a5d 编写于 作者: G Gabriel Horner

simplify and be more explicit about create and after_initialize tests

上级 f956759f
......@@ -261,26 +261,18 @@ def test_initialize_with_invalid_attribute
end
end
def test_create_with_after_initialize
wp1 = WholesaleProduct.create(:msrp => 10)
assert_equal(10, wp1.msrp)
assert_equal(5, wp1.wholesale)
wp2 = WholesaleProduct.create(:wholesale => 10)
assert_equal(20, wp2.msrp)
assert_equal(10, wp2.wholesale)
wp3 = WholesaleProduct.create do |wp|
wp.msrp = 10
end
assert_equal(10, wp3.msrp)
assert_equal(5, wp3.wholesale)
def test_create_after_initialize_without_block
wp = WholesaleProduct.create(:wholesale => 10)
assert_equal(20, wp.msrp)
assert_equal(10, wp.wholesale)
end
wp4 = WholesaleProduct.create do |wp|
wp.wholesale = 10
def test_create_after_initialize_with_block
wp = WholesaleProduct.create do |w|
w.wholesale = 10
end
assert_equal(20, wp4.msrp)
assert_equal(10, wp4.wholesale)
assert_equal(20, wp.msrp)
assert_equal(10, wp.wholesale)
end
def test_load
......
......@@ -3,11 +3,7 @@ class WholesaleProduct < ActiveRecord::Base
after_initialize :set_prices
def set_prices
if msrp.nil? && !wholesale.nil?
self.msrp = 2 * wholesale
elsif !msrp.nil? && wholesale.nil?
self.wholesale = msrp / 2
end
self.msrp = 2 * wholesale if wholesale
end
end
\ No newline at end of file
end
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册