1 require File.dirname(__FILE__) + '/../test_helper'
3 class CustomerTest < Test::Unit::TestCase
4 def test_is_invalid_when_blank
5 customer = Customer.new
6 assert !customer.valid?
7 errors = customer.errors
8 assert_equal 3, errors.size
9 assert_equal "can't be blank", errors[:name]
10 assert_equal "can't be blank", errors[:street]
11 assert_equal "can't be blank", errors[:city_id]
14 def test_is_valid_when_fully_populated
15 customer = Customer.new(:name => 'ThoughtWorks, Inc.', :street => 'Level 11, 155 Queen Street', :city => cities(:melbourne))
16 assert customer.valid?
17 assert customer.errors.empty?
20 def test_has_many_orders
21 orders = customers(:redhill).orders
23 assert orders.include?(orders(:redhill_lamps))
24 assert orders.include?(orders(:redhill_pens))
26 assert_equal 2, orders.size
29 def test_status_is_required_on_update_even_though_it_has_a_default
30 customer = customers(:redhill)
32 assert !customer.valid?
33 errors = customer.errors
34 assert_equal 1, errors.size
35 assert_equal "can't be blank", errors[:status]
39 assert_equal customers(:redhill), customers(:redhill_usa).parent
42 def test_invalid_fax_format
43 customer = Customer.new
45 assert !customer.valid?
46 assert_equal "is invalid", customer.errors[:fax]
49 def test_invalid_phone_format
50 customer = Customer.new
51 customer.phone = "abc"
52 assert !customer.valid?
53 assert_equal "is invalid", customer.errors[:phone]
56 def test_blanks_are_stripped_from_name_using_setter
57 customer = Customer.new
58 customer.name = " this is a name "
59 assert_equal "this is a name", customer.name
62 def test_blanks_are_stripped_from_name_using_indexed_setter
63 customer = Customer.new
64 customer["name"] = " this is a name "
65 assert_equal "this is a name", customer.name
68 def test_blanks_are_stripped_from_name_using_write_attribute
69 customer = Customer.new
70 customer.write_attribute("name", " this is a name ")
71 assert_equal "this is a name", customer.name
74 def test_blanks_are_stripped_from_name_using_bulk_setter
75 customer = Customer.new
76 customer.attributes = { :name => " this is a name " }
77 assert_equal "this is a name", customer.name