rspec_attribute_matchers 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/rspec_attribute_matchers.rb +66 -0
  2. metadata +4 -4
@@ -34,4 +34,70 @@ module RSpecAttributeMatchers
34
34
  "have an attribute '#{attr}'#{properties_to_sentence @extras}."
35
35
  end
36
36
  end
37
+
38
+ def has_attribute?(model, attr, extras={})
39
+ attr = attr.to_s
40
+
41
+ result = model.attributes.include? attr
42
+
43
+ if result && extras.any?
44
+ properties = model.class.columns_hash[attr]
45
+ extras.each_pair do |property, value|
46
+ result = false && break unless properties.send(property) == value
47
+ end
48
+ end
49
+
50
+ result
51
+ end
52
+
53
+ def has_association?(model, association)
54
+ model.class.reflect_on_all_associations.map { |a| a.name }.include? association.to_sym
55
+ end
56
+
57
+ RSpec::Matchers.define :validate_presence_of do |attr|
58
+ match do |model|
59
+ (has_attribute?(model, attr) || has_association?(model, attr)) &&
60
+ model._validators[attr].select { |validator| validator.instance_of? ActiveModel::Validations::PresenceValidator }.size > 0
61
+ end
62
+
63
+ failure_message_for_should do |model|
64
+ if has_attribute?(model, attr) || has_association?(model, attr)
65
+ "expected #{model.class} to validate presence of #{attr}."
66
+ else
67
+ "expected #{model.class} to have an attribute or association #{attr}."
68
+ end
69
+ end
70
+ end
71
+
72
+ RSpec::Matchers.define :validate_uniqueness_of do |attr|
73
+ match do |model|
74
+ (has_attribute?(model, attr) || has_association?(model, attr)) &&
75
+ model._validators[attr].select { |validator| validator.instance_of? ActiveRecord::Validations::UniquenessValidator }.size > 0
76
+ end
77
+
78
+ failure_message_for_should do |model|
79
+ if has_attribute?(model, attr) || has_association?(model, attr)
80
+ "expected #{model.class} to validate uniqueness of #{attr}."
81
+ else
82
+ "expected #{model.class} to have an attribute or association #{attr}."
83
+ end
84
+ end
85
+ end
86
+
87
+ RSpec::Matchers.define :validate_inclusion_of do |attr, options|
88
+ match do |model|
89
+ (has_attribute?(model, attr) || has_association?(model, attr)) &&
90
+ model._validators[attr].select do |validator|
91
+ validator.instance_of?(ActiveModel::Validations::InclusionValidator) && validator.options.merge(options) == validator.options
92
+ end.size > 0
93
+ end
94
+
95
+ failure_message_for_should do |model|
96
+ if has_attribute?(model, attr) || has_association?(model, attr)
97
+ "expected #{model.class} to validate inclusion of #{attr} in [#{options.delete(:in).map(&:to_s).join(', ')}]."
98
+ else
99
+ "expected #{model.class} to have an attribute or association #{attr}."
100
+ end
101
+ end
102
+ end
37
103
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rspec_attribute_matchers
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-10-01 00:00:00.000000000 Z
12
+ date: 2012-10-10 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description:
15
15
  email: sujoyg@gmail.com
@@ -38,8 +38,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
38
38
  version: '0'
39
39
  requirements: []
40
40
  rubyforge_project:
41
- rubygems_version: 1.8.17
41
+ rubygems_version: 1.8.24
42
42
  signing_key:
43
43
  specification_version: 3
44
- summary: RSpec matchers for writing specs for activemodel attributes.
44
+ summary: RSpec helpers for ActiveRecord models.
45
45
  test_files: []