mongoid-rspec 1.3.2 → 1.4.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/Gemfile +2 -1
- data/Gemfile.lock +22 -22
- data/README.markdown +30 -9
- data/Rakefile +2 -2
- data/VERSION +1 -1
- data/lib/matchers/associations.rb +81 -58
- data/lib/matchers/document.rb +0 -31
- data/lib/matchers/validations.rb +1 -1
- data/lib/matchers/validations/confirmation_of.rb +9 -0
- data/lib/matchers/validations/length_of.rb +93 -2
- data/lib/matchers/validations/numericality_of.rb +36 -4
- data/lib/mongoid-rspec.rb +1 -0
- data/mongoid-rspec.gemspec +11 -5
- data/spec/models/article.rb +1 -1
- data/spec/models/profile.rb +1 -1
- data/spec/models/record.rb +5 -0
- data/spec/models/user.rb +10 -9
- data/spec/unit/associations_spec.rb +22 -8
- data/spec/unit/validations_spec.rb +4 -3
- metadata +29 -11
data/Gemfile
CHANGED
@@ -1 +1,2 @@
|
|
1
|
-
|
1
|
+
source :rubygems
|
2
|
+
gemspec
|
data/Gemfile.lock
CHANGED
@@ -1,43 +1,43 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
mongoid-rspec (1.
|
5
|
-
mongoid (~> 2.0.0)
|
4
|
+
mongoid-rspec (1.4.1)
|
5
|
+
mongoid (~> 2.0.0.rc.7)
|
6
|
+
mongoid-rspec
|
6
7
|
rspec (~> 2)
|
7
8
|
|
8
9
|
GEM
|
10
|
+
remote: http://rubygems.org/
|
9
11
|
specs:
|
10
|
-
activemodel (3.0.
|
11
|
-
activesupport (= 3.0.
|
12
|
+
activemodel (3.0.5)
|
13
|
+
activesupport (= 3.0.5)
|
12
14
|
builder (~> 2.1.2)
|
13
|
-
i18n (~> 0.4
|
14
|
-
activesupport (3.0.
|
15
|
-
bson (1.
|
15
|
+
i18n (~> 0.4)
|
16
|
+
activesupport (3.0.5)
|
17
|
+
bson (1.2.4)
|
16
18
|
builder (2.1.2)
|
17
19
|
diff-lcs (1.1.2)
|
18
|
-
i18n (0.
|
19
|
-
mongo (1.
|
20
|
-
bson (>= 1.
|
21
|
-
mongoid (2.0.0.
|
20
|
+
i18n (0.5.0)
|
21
|
+
mongo (1.2.4)
|
22
|
+
bson (>= 1.2.4)
|
23
|
+
mongoid (2.0.0.rc.7)
|
22
24
|
activemodel (~> 3.0)
|
23
|
-
mongo (~> 1.
|
25
|
+
mongo (~> 1.2)
|
24
26
|
tzinfo (~> 0.3.22)
|
25
27
|
will_paginate (~> 3.0.pre)
|
26
|
-
rspec (2.
|
27
|
-
rspec-core (~> 2.
|
28
|
-
rspec-expectations (~> 2.
|
29
|
-
rspec-mocks (~> 2.
|
30
|
-
rspec-core (2.1
|
31
|
-
rspec-expectations (2.
|
28
|
+
rspec (2.5.0)
|
29
|
+
rspec-core (~> 2.5.0)
|
30
|
+
rspec-expectations (~> 2.5.0)
|
31
|
+
rspec-mocks (~> 2.5.0)
|
32
|
+
rspec-core (2.5.1)
|
33
|
+
rspec-expectations (2.5.0)
|
32
34
|
diff-lcs (~> 1.1.2)
|
33
|
-
rspec-mocks (2.
|
34
|
-
tzinfo (0.3.
|
35
|
+
rspec-mocks (2.5.0)
|
36
|
+
tzinfo (0.3.24)
|
35
37
|
will_paginate (3.0.pre2)
|
36
38
|
|
37
39
|
PLATFORMS
|
38
40
|
ruby
|
39
41
|
|
40
42
|
DEPENDENCIES
|
41
|
-
mongoid (~> 2.0.0)
|
42
43
|
mongoid-rspec!
|
43
|
-
rspec (~> 2)
|
data/README.markdown
CHANGED
@@ -3,30 +3,43 @@ mongoid-rspec
|
|
3
3
|
|
4
4
|
RSpec matchers for Mongoid.
|
5
5
|
|
6
|
-
|
7
6
|
Association Matchers
|
8
7
|
-
|
9
8
|
describe User do
|
10
|
-
it { should reference_many
|
11
|
-
it { should
|
9
|
+
it { should reference_many(:articles).with_foreign_key(:author_id) }
|
10
|
+
it { should have_many(:articles).with_foreign_key(:author_id) }
|
11
|
+
|
12
|
+
it { should reference_one(:record) }
|
13
|
+
it { should have_one(:record) }
|
14
|
+
|
15
|
+
it { should reference_many :comments }
|
16
|
+
it { should have_many :comments }
|
17
|
+
|
12
18
|
it { should embed_one :profile }
|
13
|
-
it { should reference_many(:children).stored_as(:array) }
|
14
|
-
end
|
15
19
|
|
20
|
+
it { should reference_and_be_referenced_in_many(:children).of_type(User) }
|
21
|
+
it { should have_and_belong_to_many(:children) }
|
22
|
+
end
|
23
|
+
|
16
24
|
describe Profile do
|
17
25
|
it { should be_embedded_in(:user).as_inverse_of(:profile) }
|
18
26
|
end
|
19
|
-
|
27
|
+
|
20
28
|
describe Article do
|
21
|
-
it { should be_referenced_in(:
|
29
|
+
it { should be_referenced_in(:author).of_type(User).as_inverse_of(:articles) }
|
30
|
+
it { should belong_to(:author).of_type(User).as_inverse_of(:articles) }
|
22
31
|
it { should embed_many(:comments) }
|
23
32
|
end
|
24
|
-
|
33
|
+
|
25
34
|
describe Comment do
|
26
35
|
it { should be_embedded_in(:article).as_inverse_of(:comments) }
|
27
36
|
it { should be_referenced_in(:user).as_inverse_of(:comments) }
|
28
37
|
end
|
29
38
|
|
39
|
+
describe Record do
|
40
|
+
it { should be_referenced_in(:user).as_inverse_of(:record) }
|
41
|
+
end
|
42
|
+
|
30
43
|
Validation Matchers
|
31
44
|
-
|
32
45
|
describe User do
|
@@ -36,10 +49,12 @@ Validation Matchers
|
|
36
49
|
it { should validate_associated(:profile) }
|
37
50
|
it { should validate_inclusion_of(:role).to_allow("admin", "member") }
|
38
51
|
it { should validate_numericality_of(:age) }
|
52
|
+
it { should validate_length_of(:us_phone).as_exactly(7) }
|
53
|
+
it { should validate_confirmation_of(:email) }
|
39
54
|
end
|
40
55
|
|
41
56
|
describe Article do
|
42
|
-
it { should validate_length_of(:title) }
|
57
|
+
it { should validate_length_of(:title).within(4..100) }
|
43
58
|
end
|
44
59
|
|
45
60
|
Others
|
@@ -64,3 +79,9 @@ drop in existing or dedicated support file in spec/support (spec/support/mongoid
|
|
64
79
|
RSpec.configure do |configuration|
|
65
80
|
configuration.include Mongoid::Matchers
|
66
81
|
end
|
82
|
+
|
83
|
+
Acknowledgement
|
84
|
+
-
|
85
|
+
Thanks to [Durran Jordan](https://github.com/durran) for providing the changes necessary to make
|
86
|
+
this compatible with mongoid 2.0.0.rc, and for other [contributors](https://github.com/evansagge/mongoid-rspec/contributors)
|
87
|
+
to this project.
|
data/Rakefile
CHANGED
@@ -10,7 +10,7 @@ begin
|
|
10
10
|
gem.email = "evansagge@gmail.com"
|
11
11
|
gem.homepage = "http://github.com/evansagge/mongoid-rspec"
|
12
12
|
gem.authors = ["Evan Sagge"]
|
13
|
-
gem.add_dependency "mongoid", "~> 2.0.0"
|
13
|
+
gem.add_dependency "mongoid", "~> 2.0.0.rc.7"
|
14
14
|
gem.add_dependency "rspec", "~> 2"
|
15
15
|
end
|
16
16
|
Jeweler::GemcutterTasks.new
|
@@ -32,7 +32,7 @@ RSpec::Core::RakeTask.new(:rcov) do |spec|
|
|
32
32
|
spec.rcov = true
|
33
33
|
end
|
34
34
|
|
35
|
-
task :spec => :check_dependencies
|
35
|
+
# task :spec => :check_dependencies
|
36
36
|
|
37
37
|
require 'rake/rdoctask'
|
38
38
|
Rake::RDocTask.new do |rdoc|
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
1.
|
1
|
+
1.4.1
|
@@ -1,17 +1,18 @@
|
|
1
|
-
require 'mongoid/
|
1
|
+
require 'mongoid/relations'
|
2
2
|
|
3
3
|
module Mongoid
|
4
4
|
module Matchers
|
5
5
|
module Associations
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
6
|
+
|
7
|
+
HAS_MANY = Mongoid::Relations::Referenced::Many
|
8
|
+
HAS_AND_BELONGS_TO_MANY = Mongoid::Relations::Referenced::ManyToMany
|
9
|
+
HAS_ONE = Mongoid::Relations::Referenced::One
|
10
|
+
BELONGS_TO = Mongoid::Relations::Referenced::In
|
11
|
+
EMBEDS_MANY = Mongoid::Relations::Embedded::Many
|
12
|
+
EMBEDS_ONE = Mongoid::Relations::Embedded::One
|
13
|
+
EMBEDDED_IN = Mongoid::Relations::Embedded::In
|
14
|
+
|
15
|
+
|
15
16
|
class HaveAssociationMatcher
|
16
17
|
def initialize(name, association_type)
|
17
18
|
@association = {}
|
@@ -24,13 +25,13 @@ module Mongoid
|
|
24
25
|
@expectation_message = "#{type_description} #{@association[:name].inspect}"
|
25
26
|
@expectation_message << " of type #{@association[:class].inspect}" unless @association[:class].nil?
|
26
27
|
end
|
27
|
-
|
28
|
+
|
28
29
|
def of_type(klass)
|
29
30
|
@association[:class] = klass
|
30
31
|
@expectation_message << " of type #{@association[:class].inspect}"
|
31
32
|
self
|
32
33
|
end
|
33
|
-
|
34
|
+
|
34
35
|
def as_inverse_of(association_inverse_name)
|
35
36
|
raise "#{@association[:type].inspect} does not respond to :inverse_of" unless [BELONGS_TO, EMBEDDED_IN].include?(@association[:type])
|
36
37
|
@association[:inverse_of] = association_inverse_name.to_s
|
@@ -39,60 +40,74 @@ module Mongoid
|
|
39
40
|
end
|
40
41
|
|
41
42
|
def stored_as(store_as)
|
42
|
-
@association[:
|
43
|
+
raise NotImplementedError, "`references_many #{@association[:name]} :stored_as => :array` has been removed in Mongoid 2.0.0.rc, use `references_and_referenced_in_many #{@association[:name]}` instead"
|
44
|
+
end
|
45
|
+
|
46
|
+
def with_foreign_key(foreign_key)
|
47
|
+
@association[:foreign_key] = foreign_key.to_s
|
48
|
+
@expectation_message << " using foreign key #{@association[:foreign_key].inspect}"
|
43
49
|
self
|
44
50
|
end
|
45
|
-
|
51
|
+
|
46
52
|
def matches?(actual)
|
47
53
|
@actual = actual.is_a?(Class) ? actual : actual.class
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
if association.nil?
|
54
|
+
metadata = @actual.relations[@association[:name]]
|
55
|
+
|
56
|
+
if metadata.nil?
|
52
57
|
@negative_result_message = "no association named #{@association[:name]}"
|
53
58
|
return false
|
54
59
|
else
|
55
60
|
@positive_result_message = "association named #{@association[:name]}"
|
56
61
|
end
|
57
|
-
|
58
|
-
|
59
|
-
|
62
|
+
|
63
|
+
relation = metadata.relation
|
64
|
+
if relation != @association[:type]
|
65
|
+
@negative_result_message = "#{@actual.inspect} #{type_description(relation, false)} #{@association[:name]}"
|
60
66
|
return false
|
61
67
|
else
|
62
|
-
@positive_result_message = "#{@actual.inspect} #{type_description(
|
68
|
+
@positive_result_message = "#{@actual.inspect} #{type_description(relation, false)} #{@association[:name]}"
|
63
69
|
end
|
64
|
-
|
65
|
-
if !@association[:class].nil? and @association[:class] !=
|
66
|
-
@negative_result_message = "#{@positive_result_message} of type #{
|
70
|
+
|
71
|
+
if !@association[:class].nil? and @association[:class] != metadata.klass
|
72
|
+
@negative_result_message = "#{@positive_result_message} of type #{metadata.klass.inspect}"
|
67
73
|
return false
|
68
74
|
else
|
69
|
-
@positive_result_message = "#{@positive_result_message} of type #{
|
75
|
+
@positive_result_message = "#{@positive_result_message} of type #{metadata.klass.inspect}"
|
70
76
|
end
|
71
|
-
|
77
|
+
|
72
78
|
if @association[:inverse_of]
|
73
|
-
if @association[:inverse_of].to_s !=
|
74
|
-
@negative_result_message = "#{@positive_result_message} which is an inverse of #{
|
79
|
+
if @association[:inverse_of].to_s != metadata.inverse_of.to_s
|
80
|
+
@negative_result_message = "#{@positive_result_message} which is an inverse of #{metadata.inverse_of}"
|
81
|
+
return false
|
82
|
+
else
|
83
|
+
@positive_result_message = "#{@positive_result_message} which is an inverse of #{metadata.inverse_of}"
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
if @association[:foreign_key]
|
88
|
+
if metadata.foreign_key != @association[:foreign_key]
|
89
|
+
@negative_result_message = "#{@positive_result_message} with foreign key #{metadata.foreign_key.inspect}"
|
75
90
|
return false
|
76
91
|
else
|
77
|
-
@positive_result_message = "#{@positive_result_message}
|
92
|
+
@positive_result_message = "#{@positive_result_message} with foreign key #{metadata.foreign_key.inspect}"
|
78
93
|
end
|
79
94
|
end
|
80
|
-
|
81
|
-
true
|
95
|
+
|
96
|
+
return true
|
82
97
|
end
|
83
|
-
|
84
|
-
def failure_message_for_should
|
85
|
-
"Expected #{@actual.inspect} to #{@expectation_message}, got #{@negative_result_message}"
|
86
|
-
end
|
87
|
-
|
88
|
-
def failure_message_for_should_not
|
89
|
-
"Expected #{@actual.inspect} to not #{@expectation_message}, got #{@positive_result_message}"
|
98
|
+
|
99
|
+
def failure_message_for_should
|
100
|
+
"Expected #{@actual.inspect} to #{@expectation_message}, got #{@negative_result_message}"
|
90
101
|
end
|
91
|
-
|
102
|
+
|
103
|
+
def failure_message_for_should_not
|
104
|
+
"Expected #{@actual.inspect} to not #{@expectation_message}, got #{@positive_result_message}"
|
105
|
+
end
|
106
|
+
|
92
107
|
def description
|
93
108
|
@expectation_message
|
94
109
|
end
|
95
|
-
|
110
|
+
|
96
111
|
def type_description(type = nil, passive = true)
|
97
112
|
type ||= @association[:type]
|
98
113
|
case type.name
|
@@ -106,42 +121,50 @@ module Mongoid
|
|
106
121
|
(passive ? 'reference' : 'references') << ' one'
|
107
122
|
when HAS_MANY.name
|
108
123
|
(passive ? 'reference' : 'references') << ' many'
|
109
|
-
when
|
110
|
-
(passive ? 'reference' : 'references') << '
|
124
|
+
when HAS_AND_BELONGS_TO_MANY.name
|
125
|
+
(passive ? 'reference' : 'references') << ' and referenced in many'
|
111
126
|
when BELONGS_TO.name
|
112
127
|
(passive ? 'be referenced in' : 'referenced in')
|
113
128
|
else
|
114
129
|
raise "Unknown association type '%s'" % type
|
115
130
|
end
|
116
131
|
end
|
117
|
-
end
|
118
|
-
|
132
|
+
end
|
133
|
+
|
119
134
|
def embed_one(association_name)
|
120
135
|
HaveAssociationMatcher.new(association_name, EMBEDS_ONE)
|
121
136
|
end
|
122
|
-
|
137
|
+
|
123
138
|
def embed_many(association_name)
|
124
139
|
HaveAssociationMatcher.new(association_name, EMBEDS_MANY)
|
125
|
-
end
|
126
|
-
|
140
|
+
end
|
141
|
+
|
127
142
|
def be_embedded_in(association_name)
|
128
143
|
HaveAssociationMatcher.new(association_name, EMBEDDED_IN)
|
129
144
|
end
|
130
|
-
|
145
|
+
|
131
146
|
def have_one_related(association_name)
|
132
147
|
HaveAssociationMatcher.new(association_name, HAS_ONE)
|
133
|
-
end
|
148
|
+
end
|
134
149
|
alias :reference_one :have_one_related
|
135
|
-
|
150
|
+
alias :have_one :have_one_related
|
151
|
+
|
136
152
|
def have_many_related(association_name)
|
137
153
|
HaveAssociationMatcher.new(association_name, HAS_MANY)
|
138
|
-
end
|
139
|
-
alias :reference_many :have_many_related
|
140
|
-
|
154
|
+
end
|
155
|
+
alias :reference_many :have_many_related
|
156
|
+
alias :have_many :have_many_related
|
157
|
+
|
158
|
+
def have_and_belong_to_many(association_name)
|
159
|
+
HaveAssociationMatcher.new(association_name, HAS_AND_BELONGS_TO_MANY)
|
160
|
+
end
|
161
|
+
alias :reference_and_be_referenced_in_many :have_and_belong_to_many
|
162
|
+
|
141
163
|
def belong_to_related(association_name)
|
142
164
|
HaveAssociationMatcher.new(association_name, BELONGS_TO)
|
143
|
-
end
|
144
|
-
alias :be_referenced_in :belong_to_related
|
145
|
-
|
165
|
+
end
|
166
|
+
alias :be_referenced_in :belong_to_related
|
167
|
+
alias :belong_to :belong_to_related
|
168
|
+
end
|
146
169
|
end
|
147
|
-
end
|
170
|
+
end
|
data/lib/matchers/document.rb
CHANGED
@@ -57,37 +57,6 @@ module Mongoid
|
|
57
57
|
HaveFieldMatcher.new(*args)
|
58
58
|
end
|
59
59
|
alias_method :have_fields, :have_field
|
60
|
-
|
61
|
-
class SaveMatcher
|
62
|
-
def initialize(attributes = {})
|
63
|
-
@attributes = attributes
|
64
|
-
end
|
65
|
-
|
66
|
-
def matches?(actual)
|
67
|
-
@actual = actual.is_a?(Class) ?
|
68
|
-
( defined?(::Factory) ? ::Factory.build(actual.name.underscore, @attributes) : actual.new(@attributes)) :
|
69
|
-
actual
|
70
|
-
@actual.valid? and @actual.save
|
71
|
-
end
|
72
|
-
|
73
|
-
def failure_message_for_should
|
74
|
-
"Expected #{@actual.inspect} to save properly, got #{@actual.errors.full_messages.to_sentence}"
|
75
|
-
end
|
76
|
-
|
77
|
-
def failure_message_for_should_not
|
78
|
-
"Expected #{@actual.inspect} to not save, got saved instead"
|
79
|
-
end
|
80
|
-
|
81
|
-
def description
|
82
|
-
"save properly"
|
83
|
-
end
|
84
|
-
end
|
85
|
-
|
86
|
-
def save(attributes = {})
|
87
|
-
SaveMatcher.new(attributes)
|
88
|
-
end
|
89
|
-
alias_method :save_properly, :save
|
90
|
-
|
91
60
|
end
|
92
61
|
end
|
93
62
|
|
data/lib/matchers/validations.rb
CHANGED
@@ -26,7 +26,7 @@ module Mongoid
|
|
26
26
|
|
27
27
|
def failure_message_for_should
|
28
28
|
"Expected #{@klass.inspect} to #{description}, got #{@negative_result_message}"
|
29
|
-
end
|
29
|
+
end
|
30
30
|
|
31
31
|
def failure_message_for_should_not
|
32
32
|
"Expected #{@klass.inspect} to not #{description}, got #{@positive_result_message}"
|
@@ -1,9 +1,100 @@
|
|
1
1
|
module Mongoid
|
2
2
|
module Matchers
|
3
3
|
module Validations
|
4
|
+
class ValidateLengthOfMatcher < HaveValidationMatcher
|
5
|
+
def initialize(name)
|
6
|
+
super(name, :length)
|
7
|
+
end
|
8
|
+
|
9
|
+
def with_maximum(value)
|
10
|
+
@maximum = value
|
11
|
+
self
|
12
|
+
end
|
13
|
+
|
14
|
+
def with_minimum(value)
|
15
|
+
@minimum = value
|
16
|
+
self
|
17
|
+
end
|
18
|
+
|
19
|
+
def within(value)
|
20
|
+
@within = value
|
21
|
+
self
|
22
|
+
end
|
23
|
+
alias :in :within
|
24
|
+
|
25
|
+
def as_exactly(value)
|
26
|
+
@is = value
|
27
|
+
self
|
28
|
+
end
|
29
|
+
alias :is :as_exactly
|
30
|
+
|
31
|
+
def matches?(actual)
|
32
|
+
return false unless @result = super(actual)
|
33
|
+
|
34
|
+
check_maximum if @maximum
|
35
|
+
check_minimum if @minimum
|
36
|
+
check_range if @within
|
37
|
+
check_exact if @is
|
38
|
+
|
39
|
+
@result
|
40
|
+
end
|
41
|
+
|
42
|
+
def description
|
43
|
+
options_desc = []
|
44
|
+
options_desc << " with maximum #{@maximum}" if @maximum
|
45
|
+
options_desc << " with minimum #{@minimum}" if @minimum
|
46
|
+
options_desc << " within range #{@within}" if @within
|
47
|
+
options_desc << " as exactly #{@is}" if @is
|
48
|
+
super << options_desc.to_sentence
|
49
|
+
end
|
50
|
+
|
51
|
+
private
|
52
|
+
|
53
|
+
def check_maximum
|
54
|
+
actual = @validator.options[:maximum]
|
55
|
+
if actual == @maximum
|
56
|
+
@positive_result_message << " with maximum of #{@maximum}"
|
57
|
+
else
|
58
|
+
@negative_result_message << " with maximum of #{actual}"
|
59
|
+
@result = false
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
def check_minimum
|
64
|
+
actual = @validator.options[:minimum]
|
65
|
+
if actual == @minimum
|
66
|
+
@positive_result_message << " with minimum of #{@minimum}"
|
67
|
+
else
|
68
|
+
@negative_result_message << " with minimum of #{actual}"
|
69
|
+
@result = false
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
def check_range
|
74
|
+
min, max = [@within.min, @within.max]
|
75
|
+
actual = @validator.options
|
76
|
+
if actual[:minimum] == min && actual[:maximum] == max
|
77
|
+
@positive_result_message << " with range #{@within.inspect}"
|
78
|
+
else
|
79
|
+
@negative_result_message << " with range #{(actual[:minimum]..actual[:maximum]).inspect}"
|
80
|
+
@result = false
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
def check_exact
|
85
|
+
actual = @validator.options[:is]
|
86
|
+
if actual == @is
|
87
|
+
@positive_result_message << " is exactly #{@is}"
|
88
|
+
else
|
89
|
+
@negative_result_message << " is exactly #{actual}"
|
90
|
+
@result = false
|
91
|
+
end
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
4
95
|
def validate_length_of(field)
|
5
|
-
|
96
|
+
ValidateLengthOfMatcher.new(field)
|
6
97
|
end
|
7
98
|
end
|
8
99
|
end
|
9
|
-
end
|
100
|
+
end
|
@@ -1,9 +1,41 @@
|
|
1
1
|
module Mongoid
|
2
2
|
module Matchers
|
3
|
-
module Validations
|
3
|
+
module Validations
|
4
|
+
class ValidateNumericalityOfMatcher < HaveValidationMatcher
|
5
|
+
def initialize(field)
|
6
|
+
super(field, :numericality)
|
7
|
+
end
|
8
|
+
|
9
|
+
def greater_than(value)
|
10
|
+
@greater_than = value
|
11
|
+
self
|
12
|
+
end
|
13
|
+
|
14
|
+
def matches?(actual)
|
15
|
+
return false unless result = super(actual)
|
16
|
+
|
17
|
+
if @greater_than
|
18
|
+
if @validator.options[:greater_than] == @greater_than
|
19
|
+
@positive_result_message = @positive_result_message << " checking if values are greater than #{@validator.options[:greater_than].inspect}"
|
20
|
+
else
|
21
|
+
@negative_result_message = @negative_result_message << " checking if values are greater than #{@validator.options[:greater_than].inspect}"
|
22
|
+
result = false
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
result
|
27
|
+
end
|
28
|
+
|
29
|
+
def description
|
30
|
+
options_desc = []
|
31
|
+
options_desc << " allowing values greater than #{@greater_than}" if @greater_than
|
32
|
+
super << options_desc.to_sentence
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
4
36
|
def validate_numericality_of(field)
|
5
|
-
|
6
|
-
end
|
37
|
+
ValidateNumericalityOfMatcher.new(field)
|
38
|
+
end
|
7
39
|
end
|
8
40
|
end
|
9
|
-
end
|
41
|
+
end
|
data/lib/mongoid-rspec.rb
CHANGED
@@ -6,6 +6,7 @@ require 'matchers/document'
|
|
6
6
|
require 'matchers/associations'
|
7
7
|
require 'matchers/validations'
|
8
8
|
require 'matchers/validations/associated'
|
9
|
+
require 'matchers/validations/confirmation_of'
|
9
10
|
require 'matchers/validations/format_of'
|
10
11
|
require 'matchers/validations/inclusion_of'
|
11
12
|
require 'matchers/validations/length_of'
|
data/mongoid-rspec.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{mongoid-rspec}
|
8
|
-
s.version = "1.
|
8
|
+
s.version = "1.4.1"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Evan Sagge"]
|
12
|
-
s.date = %q{
|
12
|
+
s.date = %q{2011-03-03}
|
13
13
|
s.description = %q{RSpec matches for Mongoid models, including association and validation matchers}
|
14
14
|
s.email = %q{evansagge@gmail.com}
|
15
15
|
s.extra_rdoc_files = [
|
@@ -30,6 +30,7 @@ Gem::Specification.new do |s|
|
|
30
30
|
"lib/matchers/document.rb",
|
31
31
|
"lib/matchers/validations.rb",
|
32
32
|
"lib/matchers/validations/associated.rb",
|
33
|
+
"lib/matchers/validations/confirmation_of.rb",
|
33
34
|
"lib/matchers/validations/format_of.rb",
|
34
35
|
"lib/matchers/validations/inclusion_of.rb",
|
35
36
|
"lib/matchers/validations/length_of.rb",
|
@@ -41,6 +42,7 @@ Gem::Specification.new do |s|
|
|
41
42
|
"spec/models/article.rb",
|
42
43
|
"spec/models/comment.rb",
|
43
44
|
"spec/models/profile.rb",
|
45
|
+
"spec/models/record.rb",
|
44
46
|
"spec/models/site.rb",
|
45
47
|
"spec/models/user.rb",
|
46
48
|
"spec/spec_helper.rb",
|
@@ -56,6 +58,7 @@ Gem::Specification.new do |s|
|
|
56
58
|
"spec/models/article.rb",
|
57
59
|
"spec/models/comment.rb",
|
58
60
|
"spec/models/profile.rb",
|
61
|
+
"spec/models/record.rb",
|
59
62
|
"spec/models/site.rb",
|
60
63
|
"spec/models/user.rb",
|
61
64
|
"spec/spec_helper.rb",
|
@@ -69,14 +72,17 @@ Gem::Specification.new do |s|
|
|
69
72
|
s.specification_version = 3
|
70
73
|
|
71
74
|
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
72
|
-
s.add_runtime_dependency(%q<mongoid>, ["
|
75
|
+
s.add_runtime_dependency(%q<mongoid-rspec>, [">= 0"])
|
76
|
+
s.add_runtime_dependency(%q<mongoid>, ["~> 2.0.0.rc.7"])
|
73
77
|
s.add_runtime_dependency(%q<rspec>, ["~> 2"])
|
74
78
|
else
|
75
|
-
s.add_dependency(%q<mongoid>, ["
|
79
|
+
s.add_dependency(%q<mongoid-rspec>, [">= 0"])
|
80
|
+
s.add_dependency(%q<mongoid>, ["~> 2.0.0.rc.7"])
|
76
81
|
s.add_dependency(%q<rspec>, ["~> 2"])
|
77
82
|
end
|
78
83
|
else
|
79
|
-
s.add_dependency(%q<mongoid>, ["
|
84
|
+
s.add_dependency(%q<mongoid-rspec>, [">= 0"])
|
85
|
+
s.add_dependency(%q<mongoid>, ["~> 2.0.0.rc.7"])
|
80
86
|
s.add_dependency(%q<rspec>, ["~> 2"])
|
81
87
|
end
|
82
88
|
end
|
data/spec/models/article.rb
CHANGED
data/spec/models/profile.rb
CHANGED
data/spec/models/user.rb
CHANGED
@@ -1,23 +1,24 @@
|
|
1
1
|
class User
|
2
2
|
include Mongoid::Document
|
3
|
-
|
3
|
+
|
4
4
|
field :login
|
5
5
|
field :email
|
6
6
|
field :role
|
7
|
-
|
7
|
+
|
8
8
|
referenced_in :site, :inverse_of => :users
|
9
|
-
references_many :articles
|
9
|
+
references_many :articles, :foreign_key => :author_id
|
10
10
|
references_many :comments
|
11
|
-
|
12
|
-
|
11
|
+
references_and_referenced_in_many :children, :class_name => "User"
|
12
|
+
references_one :record
|
13
|
+
|
13
14
|
embeds_one :profile
|
14
|
-
|
15
|
+
|
15
16
|
validates :login, :presence => true, :uniqueness => { :scope => :site }, :format => { :with => /^[\w\-]+$/ }
|
16
|
-
validates :email, :uniqueness => { :case_sensitive => false, :scope => :site, :message => "is already taken" }
|
17
|
+
validates :email, :uniqueness => { :case_sensitive => false, :scope => :site, :message => "is already taken" }, :confirmation => true
|
17
18
|
validates :role, :presence => true, :inclusion => { :in => ["admin", "moderator", "member"]}
|
18
19
|
validates :profile, :presence => true, :associated => true
|
19
|
-
|
20
|
+
|
20
21
|
def admin?
|
21
22
|
false
|
22
23
|
end
|
23
|
-
end
|
24
|
+
end
|
@@ -2,23 +2,37 @@ require 'spec_helper'
|
|
2
2
|
|
3
3
|
describe "Associations" do
|
4
4
|
describe User do
|
5
|
-
it { should reference_many
|
6
|
-
it { should
|
5
|
+
it { should reference_many(:articles).with_foreign_key(:author_id) }
|
6
|
+
it { should have_many(:articles).with_foreign_key(:author_id) }
|
7
|
+
|
8
|
+
it { should reference_one(:record) }
|
9
|
+
it { should have_one(:record) }
|
10
|
+
|
11
|
+
it { should reference_many :comments }
|
12
|
+
it { should have_many :comments }
|
13
|
+
|
7
14
|
it { should embed_one :profile }
|
8
|
-
|
15
|
+
|
16
|
+
it { should reference_and_be_referenced_in_many(:children).of_type(User) }
|
17
|
+
it { should have_and_belong_to_many(:children) }
|
9
18
|
end
|
10
|
-
|
19
|
+
|
11
20
|
describe Profile do
|
12
21
|
it { should be_embedded_in(:user).as_inverse_of(:profile) }
|
13
22
|
end
|
14
|
-
|
23
|
+
|
15
24
|
describe Article do
|
16
|
-
it { should be_referenced_in(:
|
25
|
+
it { should be_referenced_in(:author).of_type(User).as_inverse_of(:articles) }
|
26
|
+
it { should belong_to(:author).of_type(User).as_inverse_of(:articles) }
|
17
27
|
it { should embed_many(:comments) }
|
18
28
|
end
|
19
|
-
|
29
|
+
|
20
30
|
describe Comment do
|
21
31
|
it { should be_embedded_in(:article).as_inverse_of(:comments) }
|
22
32
|
it { should be_referenced_in(:user).as_inverse_of(:comments) }
|
23
33
|
end
|
24
|
-
|
34
|
+
|
35
|
+
describe Record do
|
36
|
+
it { should be_referenced_in(:user).as_inverse_of(:record) }
|
37
|
+
end
|
38
|
+
end
|
@@ -13,13 +13,14 @@ describe "Validations" do
|
|
13
13
|
it { should validate_format_of(:login).to_allow("valid_login").not_to_allow("invalid login") }
|
14
14
|
it { should validate_associated(:profile) }
|
15
15
|
it { should validate_inclusion_of(:role).to_allow("admin", "member") }
|
16
|
+
it { should validate_confirmation_of(:email) }
|
16
17
|
end
|
17
18
|
|
18
19
|
describe Profile do
|
19
|
-
it { should validate_numericality_of(:age) }
|
20
|
+
it { should validate_numericality_of(:age).greater_than(0) }
|
20
21
|
end
|
21
22
|
|
22
23
|
describe Article do
|
23
|
-
it { should validate_length_of(:title) }
|
24
|
+
it { should validate_length_of(:title).within(8..16) }
|
24
25
|
end
|
25
|
-
end
|
26
|
+
end
|
metadata
CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
|
|
4
4
|
prerelease: false
|
5
5
|
segments:
|
6
6
|
- 1
|
7
|
-
-
|
8
|
-
-
|
9
|
-
version: 1.
|
7
|
+
- 4
|
8
|
+
- 1
|
9
|
+
version: 1.4.1
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Evan Sagge
|
@@ -14,13 +14,25 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date:
|
17
|
+
date: 2011-03-03 00:00:00 +08:00
|
18
18
|
default_executable:
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
21
|
-
name: mongoid
|
22
|
-
prerelease: false
|
21
|
+
name: mongoid-rspec
|
23
22
|
requirement: &id001 !ruby/object:Gem::Requirement
|
23
|
+
none: false
|
24
|
+
requirements:
|
25
|
+
- - ">="
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
segments:
|
28
|
+
- 0
|
29
|
+
version: "0"
|
30
|
+
type: :runtime
|
31
|
+
prerelease: false
|
32
|
+
version_requirements: *id001
|
33
|
+
- !ruby/object:Gem::Dependency
|
34
|
+
name: mongoid
|
35
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
24
36
|
none: false
|
25
37
|
requirements:
|
26
38
|
- - ~>
|
@@ -29,13 +41,15 @@ dependencies:
|
|
29
41
|
- 2
|
30
42
|
- 0
|
31
43
|
- 0
|
32
|
-
|
44
|
+
- rc
|
45
|
+
- 7
|
46
|
+
version: 2.0.0.rc.7
|
33
47
|
type: :runtime
|
34
|
-
|
48
|
+
prerelease: false
|
49
|
+
version_requirements: *id002
|
35
50
|
- !ruby/object:Gem::Dependency
|
36
51
|
name: rspec
|
37
|
-
|
38
|
-
requirement: &id002 !ruby/object:Gem::Requirement
|
52
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
39
53
|
none: false
|
40
54
|
requirements:
|
41
55
|
- - ~>
|
@@ -44,7 +58,8 @@ dependencies:
|
|
44
58
|
- 2
|
45
59
|
version: "2"
|
46
60
|
type: :runtime
|
47
|
-
|
61
|
+
prerelease: false
|
62
|
+
version_requirements: *id003
|
48
63
|
description: RSpec matches for Mongoid models, including association and validation matchers
|
49
64
|
email: evansagge@gmail.com
|
50
65
|
executables: []
|
@@ -68,6 +83,7 @@ files:
|
|
68
83
|
- lib/matchers/document.rb
|
69
84
|
- lib/matchers/validations.rb
|
70
85
|
- lib/matchers/validations/associated.rb
|
86
|
+
- lib/matchers/validations/confirmation_of.rb
|
71
87
|
- lib/matchers/validations/format_of.rb
|
72
88
|
- lib/matchers/validations/inclusion_of.rb
|
73
89
|
- lib/matchers/validations/length_of.rb
|
@@ -79,6 +95,7 @@ files:
|
|
79
95
|
- spec/models/article.rb
|
80
96
|
- spec/models/comment.rb
|
81
97
|
- spec/models/profile.rb
|
98
|
+
- spec/models/record.rb
|
82
99
|
- spec/models/site.rb
|
83
100
|
- spec/models/user.rb
|
84
101
|
- spec/spec_helper.rb
|
@@ -121,6 +138,7 @@ test_files:
|
|
121
138
|
- spec/models/article.rb
|
122
139
|
- spec/models/comment.rb
|
123
140
|
- spec/models/profile.rb
|
141
|
+
- spec/models/record.rb
|
124
142
|
- spec/models/site.rb
|
125
143
|
- spec/models/user.rb
|
126
144
|
- spec/spec_helper.rb
|