mongoid-rspec 1.8.0 → 1.8.1
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/matchers/validations/custom_validation_of.rb +2 -19
- data/lib/matchers/validations/length_of.rb +22 -0
- data/lib/matchers/validations/uniqueness_of.rb +2 -19
- data/lib/matchers/validations/with_message.rb +27 -0
- data/lib/mongoid-rspec.rb +1 -0
- data/lib/mongoid-rspec/version.rb +1 -1
- data/spec/models/profile.rb +3 -1
- data/spec/unit/validations_spec.rb +1 -0
- metadata +5 -4
@@ -2,6 +2,7 @@ module Mongoid
|
|
2
2
|
module Matchers
|
3
3
|
module Validations
|
4
4
|
class ValidateWithCustomValidatorMatcher < HaveValidationMatcher
|
5
|
+
include WithMessage
|
5
6
|
def initialize(field)
|
6
7
|
super(field, :custom)
|
7
8
|
end
|
@@ -11,11 +12,6 @@ module Mongoid
|
|
11
12
|
self
|
12
13
|
end
|
13
14
|
|
14
|
-
def with_message(message)
|
15
|
-
@expected_message = message
|
16
|
-
self
|
17
|
-
end
|
18
|
-
|
19
15
|
def matches?(actual)
|
20
16
|
return false unless (@result = super(actual))
|
21
17
|
check_custom_validator if @custom_validator
|
@@ -41,19 +37,6 @@ module Mongoid
|
|
41
37
|
@result = false
|
42
38
|
end
|
43
39
|
end
|
44
|
-
|
45
|
-
def check_expected_message
|
46
|
-
actual_message = @validator.options[:message]
|
47
|
-
if actual_message.nil?
|
48
|
-
@negative_result_message << " with no custom message"
|
49
|
-
@result = false
|
50
|
-
elsif actual_message == @expected_message
|
51
|
-
@positive_result_message << " with custom message '#{@expected_message}'"
|
52
|
-
else
|
53
|
-
@negative_result_message << " got message '#{actual_message}'"
|
54
|
-
@result = false
|
55
|
-
end
|
56
|
-
end
|
57
40
|
end
|
58
41
|
|
59
42
|
def custom_validate(field)
|
@@ -61,4 +44,4 @@ module Mongoid
|
|
61
44
|
end
|
62
45
|
end
|
63
46
|
end
|
64
|
-
end
|
47
|
+
end
|
@@ -2,6 +2,8 @@ module Mongoid
|
|
2
2
|
module Matchers
|
3
3
|
module Validations
|
4
4
|
class ValidateLengthOfMatcher < HaveValidationMatcher
|
5
|
+
include WithMessage
|
6
|
+
|
5
7
|
def initialize(name)
|
6
8
|
super(name, :length)
|
7
9
|
end
|
@@ -30,6 +32,11 @@ module Mongoid
|
|
30
32
|
end
|
31
33
|
alias :is :as_exactly
|
32
34
|
|
35
|
+
def with_message(message)
|
36
|
+
@expected_message = message
|
37
|
+
self
|
38
|
+
end
|
39
|
+
|
33
40
|
def matches?(actual)
|
34
41
|
return false unless @result = super(actual)
|
35
42
|
|
@@ -37,6 +44,7 @@ module Mongoid
|
|
37
44
|
check_minimum if @minimum
|
38
45
|
check_range if @within
|
39
46
|
check_exact if @is
|
47
|
+
check_expected_message if @expected_message
|
40
48
|
|
41
49
|
@result
|
42
50
|
end
|
@@ -47,6 +55,7 @@ module Mongoid
|
|
47
55
|
options_desc << "with maximum of #{@maximum}" if @maximum
|
48
56
|
options_desc << "within the range of #{@within}" if @within
|
49
57
|
options_desc << "as exactly #{@is}" if @is
|
58
|
+
options_desc << "with message '#{@expected_message}'" if @expected_message
|
50
59
|
super << " #{options_desc.to_sentence}"
|
51
60
|
end
|
52
61
|
|
@@ -104,6 +113,19 @@ module Mongoid
|
|
104
113
|
end
|
105
114
|
end
|
106
115
|
|
116
|
+
def check_expected_message
|
117
|
+
actual_message = @validator.options[:message]
|
118
|
+
if actual_message.nil?
|
119
|
+
@negative_result_message << " with no custom message"
|
120
|
+
@result = false
|
121
|
+
elsif actual_message == @expected_message
|
122
|
+
@positive_result_message << " with custom message '#{@expected_message}'"
|
123
|
+
else
|
124
|
+
@negative_result_message << " got message '#{actual_message}'"
|
125
|
+
@result = false
|
126
|
+
end
|
127
|
+
end
|
128
|
+
|
107
129
|
def actual_is
|
108
130
|
actual_is = @validator.options[:is]
|
109
131
|
end
|
@@ -2,6 +2,7 @@ module Mongoid
|
|
2
2
|
module Matchers
|
3
3
|
module Validations
|
4
4
|
class ValidateUniquenessOfMatcher < HaveValidationMatcher
|
5
|
+
include WithMessage
|
5
6
|
def initialize(field)
|
6
7
|
super(field, :uniqueness)
|
7
8
|
end
|
@@ -22,11 +23,6 @@ module Mongoid
|
|
22
23
|
self
|
23
24
|
end
|
24
25
|
|
25
|
-
def with_message(message)
|
26
|
-
@expected_message = message
|
27
|
-
self
|
28
|
-
end
|
29
|
-
|
30
26
|
def matches?(actual)
|
31
27
|
return false unless @result = super(actual)
|
32
28
|
|
@@ -76,19 +72,6 @@ module Mongoid
|
|
76
72
|
@result = false
|
77
73
|
end
|
78
74
|
end
|
79
|
-
|
80
|
-
def check_expected_message
|
81
|
-
actual_message = @validator.options[:message]
|
82
|
-
if actual_message.nil?
|
83
|
-
@negative_result_message << " with no custom message"
|
84
|
-
@result = false
|
85
|
-
elsif actual_message == @expected_message
|
86
|
-
@positive_result_message << " with custom message '#{@expected_message}'"
|
87
|
-
else
|
88
|
-
@negative_result_message << " got message '#{actual_message}'"
|
89
|
-
@result = false
|
90
|
-
end
|
91
|
-
end
|
92
75
|
end
|
93
76
|
|
94
77
|
def validate_uniqueness_of(field)
|
@@ -96,4 +79,4 @@ module Mongoid
|
|
96
79
|
end
|
97
80
|
end
|
98
81
|
end
|
99
|
-
end
|
82
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
module Mongoid
|
2
|
+
module Matchers
|
3
|
+
module Validations
|
4
|
+
module WithMessage
|
5
|
+
def with_message(message)
|
6
|
+
@expected_message = message
|
7
|
+
self
|
8
|
+
end
|
9
|
+
|
10
|
+
private
|
11
|
+
|
12
|
+
def check_expected_message
|
13
|
+
actual_message = @validator.options[:message]
|
14
|
+
if actual_message.nil?
|
15
|
+
@negative_result_message << " with no custom message"
|
16
|
+
@result = false
|
17
|
+
elsif actual_message == @expected_message
|
18
|
+
@positive_result_message << " with custom message '#{@expected_message}'"
|
19
|
+
else
|
20
|
+
@negative_result_message << " got message '#{actual_message}'"
|
21
|
+
@result = false
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
data/lib/mongoid-rspec.rb
CHANGED
@@ -10,6 +10,7 @@ require 'matchers/indexes'
|
|
10
10
|
require 'matchers/allow_mass_assignment'
|
11
11
|
require 'matchers/accept_nested_attributes'
|
12
12
|
require 'matchers/validations'
|
13
|
+
require 'matchers/validations/with_message'
|
13
14
|
require 'matchers/validations/associated'
|
14
15
|
require 'matchers/validations/confirmation_of'
|
15
16
|
require 'matchers/validations/exclusion_of'
|
data/spec/models/profile.rb
CHANGED
@@ -4,11 +4,13 @@ class Profile
|
|
4
4
|
field :first_name
|
5
5
|
field :last_name
|
6
6
|
field :age
|
7
|
-
|
7
|
+
field :hobbies, type: Array, default: []
|
8
|
+
|
8
9
|
embedded_in :user, :inverse_of => :profile
|
9
10
|
|
10
11
|
validates_numericality_of :age, :greater_than => 0
|
11
12
|
validates_acceptance_of :terms_of_service
|
13
|
+
validates_length_of :hobbies, minimum: 1, message: "requires at least one hobby"
|
12
14
|
|
13
15
|
index({first_name: 1, last_name: 1 })
|
14
16
|
end
|
@@ -26,6 +26,7 @@ describe "Validations" do
|
|
26
26
|
describe Profile do
|
27
27
|
it { should validate_numericality_of(:age).greater_than(0) }
|
28
28
|
it { should validate_acceptance_of(:terms_of_service) }
|
29
|
+
it { should validate_length_of(:hobbies).with_minimum(1).with_message("requires at least one hobby") }
|
29
30
|
end
|
30
31
|
|
31
32
|
describe Article do
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mongoid-rspec
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.8.
|
4
|
+
version: 1.8.1
|
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: 2013-
|
12
|
+
date: 2013-05-03 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rake
|
@@ -93,6 +93,7 @@ files:
|
|
93
93
|
- lib/matchers/validations/numericality_of.rb
|
94
94
|
- lib/matchers/validations/presence_of.rb
|
95
95
|
- lib/matchers/validations/uniqueness_of.rb
|
96
|
+
- lib/matchers/validations/with_message.rb
|
96
97
|
- lib/mongoid-rspec.rb
|
97
98
|
- lib/mongoid-rspec/version.rb
|
98
99
|
- mongoid-rspec.gemspec
|
@@ -129,7 +130,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
129
130
|
version: '0'
|
130
131
|
segments:
|
131
132
|
- 0
|
132
|
-
hash:
|
133
|
+
hash: 3977837244165332206
|
133
134
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
134
135
|
none: false
|
135
136
|
requirements:
|
@@ -138,7 +139,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
138
139
|
version: '0'
|
139
140
|
segments:
|
140
141
|
- 0
|
141
|
-
hash:
|
142
|
+
hash: 3977837244165332206
|
142
143
|
requirements: []
|
143
144
|
rubyforge_project: mongoid-rspec
|
144
145
|
rubygems_version: 1.8.25
|