This repository has been archived by the owner on Oct 22, 2019. It is now read-only.
/
validation_expectations.rb
59 lines (52 loc) · 2.19 KB
/
validation_expectations.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
def it_should_validate_presence_of(*one_or_more_fields)
model_name = described_type
one_or_more_fields.each do |field|
it "should validate presence of #{field.to_s.humanize.downcase}" do
validations = model_name.reflect_on_all_validations
validations = validations.select { |e| e.macro == :validates_presence_of }
field_names = validations.collect(&:name)
field_names.should include(field)
end
end
end
def it_should_validate_numericality_of(*one_or_more_fields)
model_name = described_type
one_or_more_fields.each do |field|
it "should validate numericality of #{field.to_s.humanize.downcase}" do
validations = model_name.reflect_on_all_validations
validations = validations.select { |e| e.macro == :validates_numericality_of }
field_names = validations.collect(&:name)
field_names.should include(field)
end
end
end
def it_should_validate_uniqueness_of(*one_or_more_fields)
options = one_or_more_fields.last.is_a?(Hash) ? one_or_more_fields.pop : {}
model_name = described_type
one_or_more_fields.each do |field|
it "should validate uniqueness of #{field.to_s.humanize.downcase}" do
validations = model_name.reflect_on_all_validations
validations = validations.select { |e| e.macro == :validates_uniqueness_of }
validations.collect(&:name).should include(field)
validations.collect(&:options).should include(options)
end
end
end
def it_should_validate_inclusion_of(field_name, *args)
model_name = described_type
options = args.last.is_a?(Hash) ? args.pop : {}
it "should validate inclusion of #{field_name} as one of #{options[:in].to_sentence(:connector => 'or', :skip_last_comma => true)}" do
validations = model_name.reflect_on_all_validations
validation = validations.detect {|v| v.macro == :validates_inclusion_of && v.name == field_name}
unless validation.nil?
validation.options[:in].sort.should == options[:in].sort
end
end
end
def it_should_be_createable *args
model_name = described_type
attributes = args.last.is_a?(Hash) ? args.last[:with] : {}
it "should be creatable" do
lambda {model_name.create(attributes)}.should change(model_name, :count).by(1)
end
end