This repository has been archived by the owner on Nov 11, 2017. It is now read-only.
/
validations_test.rb
83 lines (68 loc) · 3.53 KB
/
validations_test.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
require File.join(File.dirname(__FILE__), 'test_helper')
class ValidationsTest < Test::Unit::TestCase
def setup
load File.join(File.dirname(__FILE__), 'fixtures', 'company.rb')
end
basic_validations = %w(validate validate_on_create)
update_validations = %w(validate_on_update)
conditions = [lambda {|company| company.callback_flag == true}]
conditions.each do |condition|
basic_validations.each do |validation|
define_method "test_#{validation}_with_if_condition_#{condition.class}_which_returns_true_should_change_company_name" do
Company.send(validation.to_sym, :change_name, :if => condition)
company = Company.new :name => 'thoughtbot', :callback_flag => true
assert company.save
assert_equal 'new name', company.name
end
define_method "test_#{validation}_with_if_condition_#{condition.class}_which_returns_false_should_not_change_company_name" do
Company.send(validation.to_sym, :change_name, :if => condition)
company = Company.new :name => 'thoughtbot', :callback_flag => false
assert company.save
assert_equal 'thoughtbot', company.name
end
define_method "test_#{validation}_with_unless_condition_#{condition.class}_which_returns_true_should_not_change_company_name" do
Company.send(validation.to_sym, :change_name, :unless => condition)
company = Company.new :name => 'thoughtbot', :callback_flag => true
assert company.save
assert_equal 'thoughtbot', company.name
end
define_method "test_#{validation}_with_unless_condition_#{condition.class}_which_returns_false_should_change_company_name" do
Company.send(validation.to_sym, :change_name, :unless => condition)
company = Company.new :name => 'thoughtbot', :callback_flag => false
assert company.save
assert_equal 'new name', company.name
end
end
update_validations.each do |validation|
define_method "test_#{validation}_with_if_condition_#{condition.class}_which_returns_true_should_change_company_name" do
Company.send(validation.to_sym, :change_name, :if => condition)
company = Company.create :name => 'thoughtbot', :callback_flag => true
assert company.save
assert_equal 'new name', company.name
end
define_method "test_#{validation}_with_if_condition_#{condition.class}_which_returns_false_should_not_change_company_name" do
Company.send(validation.to_sym, :change_name, :if => condition)
company = Company.create :name => 'thoughtbot', :callback_flag => false
assert company.save
assert_equal 'thoughtbot', company.name
end
define_method "test_#{validation}_with_unless_condition_#{condition.class}_which_returns_true_should_not_change_company_name" do
Company.send(validation.to_sym, :change_name, :unless => condition)
company = Company.create :name => 'thoughtbot', :callback_flag => true
assert company.save
assert_equal 'thoughtbot', company.name
end
define_method "test_#{validation}_with_unless_condition_#{condition.class}_which_returns_false_should_change_company_name" do
Company.send(validation.to_sym, :change_name, :unless => condition)
company = Company.create :name => 'thoughtbot', :callback_flag => false
assert company.save
assert_equal 'new name', company.name
end
end
end
def teardown
Object.class_eval do
remove_const Company.to_s if const_defined? Company.to_s
end
end
end