mongoid-rspec 1.5.0 → 1.5.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +25 -20
- data/lib/matchers/document.rb +15 -2
- data/lib/matchers/validations.rb +32 -25
- data/lib/mongoid-rspec/version.rb +1 -1
- data/spec/models/user.rb +4 -1
- data/spec/unit/document_spec.rb +5 -5
- data/spec/unit/validations_spec.rb +1 -0
- metadata +7 -7
data/README.md
CHANGED
@@ -1,25 +1,29 @@
|
|
1
1
|
mongoid-rspec
|
2
2
|
=
|
3
3
|
|
4
|
-
|
4
|
+
http://rubygems.org/gems/mongoid-rspec
|
5
|
+
|
6
|
+
RSpec matchers for Mongoid 3.x.
|
7
|
+
|
8
|
+
For Mongoid 2.x, use [mongoid-rspec 1.4.5](http://rubygems.org/gems/mongoid-rspec/versions/1.4.5)
|
5
9
|
|
6
10
|
Association Matchers
|
7
11
|
-
|
8
12
|
describe User do
|
9
13
|
it { should have_many(:articles).with_foreign_key(:author_id) }
|
10
|
-
|
11
|
-
it { should have_one(:record) }
|
12
|
-
|
14
|
+
|
15
|
+
it { should have_one(:record) }
|
16
|
+
|
13
17
|
it { should have_many :comments }
|
14
|
-
|
18
|
+
|
15
19
|
#can also specify with_dependent to test if :dependent => :destroy/:destroy_all/:delete is set
|
16
20
|
it { should have_many(:comments).with_dependent(:destroy) }
|
17
21
|
#can verify autosave is set to true
|
18
22
|
it { should have_many(:comments).with_autosave }
|
19
|
-
|
23
|
+
|
20
24
|
it { should embed_one :profile }
|
21
25
|
|
22
|
-
it { should have_and_belong_to_many(:children) }
|
26
|
+
it { should have_and_belong_to_many(:children) }
|
23
27
|
it { should have_and_belong_to_many(:children).of_type(User) }
|
24
28
|
end
|
25
29
|
|
@@ -68,14 +72,14 @@ Validation Matchers
|
|
68
72
|
describe Article do
|
69
73
|
it { should validate_length_of(:title).within(8..16) }
|
70
74
|
end
|
71
|
-
|
75
|
+
|
72
76
|
describe Profile do
|
73
77
|
it { should validate_numericality_of(:age).greater_than(0) }
|
74
|
-
end
|
78
|
+
end
|
75
79
|
|
76
80
|
describe MovieArticle do
|
77
81
|
it { should validate_numericality_of(:rating).to_allow(:greater_than => 0).less_than_or_equal_to(5) }
|
78
|
-
it { should validate_numericality_of(:classification).to_allow(:even => true, :only_integer => true, :nil => false) }
|
82
|
+
it { should validate_numericality_of(:classification).to_allow(:even => true, :only_integer => true, :nil => false) }
|
79
83
|
end
|
80
84
|
|
81
85
|
describe Person do
|
@@ -93,33 +97,34 @@ Others
|
|
93
97
|
it { should have_index_for(:last_name) }
|
94
98
|
it { should have_index_for(:email).with_options(:unique => true) }
|
95
99
|
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
it {
|
100
|
+
it { should be_timestamped_document } # if you're declaring `include
|
101
|
+
Mongoid::Timestamps` or any of `include Mongoid::Timestamps::Created` and `Mongoid::Timestamps::Updated`
|
102
|
+
it { should be_timestamped_document.with(:created) }
|
103
|
+
it { should_not be_timestamped_document.with(:updated) }
|
104
|
+
|
100
105
|
it { should be_versioned_document } # if you're declaring `include Mongoid::Versioning`
|
101
106
|
it { should be_paranoid_document } # if you're declaring `include Mongoid::Paranoia`
|
102
107
|
it { should be_multiparameted_document } # if you're declaring `include Mongoid::MultiParameterAttributes`
|
103
108
|
end
|
104
|
-
|
109
|
+
|
105
110
|
describe Log do
|
106
111
|
it { should be_stored_in :logs }
|
107
|
-
end
|
112
|
+
end
|
108
113
|
|
109
114
|
Use
|
110
115
|
-
|
111
116
|
add in Gemfile
|
112
117
|
|
113
118
|
gem 'mongoid-rspec'
|
114
|
-
|
119
|
+
|
115
120
|
drop in existing or dedicated support file in spec/support (spec/support/mongoid.rb)
|
116
121
|
|
117
122
|
RSpec.configure do |configuration|
|
118
123
|
configuration.include Mongoid::Matchers
|
119
124
|
end
|
120
|
-
|
125
|
+
|
121
126
|
Acknowledgement
|
122
127
|
-
|
123
|
-
Thanks to [Durran Jordan](https://github.com/durran) for providing the changes necessary to make
|
124
|
-
this compatible with mongoid 2.0.0.rc, and for other [contributors](https://github.com/evansagge/mongoid-rspec/contributors)
|
128
|
+
Thanks to [Durran Jordan](https://github.com/durran) for providing the changes necessary to make
|
129
|
+
this compatible with mongoid 2.0.0.rc, and for other [contributors](https://github.com/evansagge/mongoid-rspec/contributors)
|
125
130
|
to this project.
|
data/lib/matchers/document.rb
CHANGED
@@ -92,11 +92,24 @@ end
|
|
92
92
|
|
93
93
|
RSpec::Matchers.define :be_timestamped_document do
|
94
94
|
match do |doc|
|
95
|
-
|
95
|
+
if [*@timestamped_module].any?
|
96
|
+
modules = [*@timestamped_module].map{|m| "Mongoid::Timestamps::#{m.to_s.classify}".constantize }
|
97
|
+
(modules - doc.class.included_modules).empty?
|
98
|
+
else
|
99
|
+
doc.class.included_modules.include?(Mongoid::Timestamps) or
|
100
|
+
doc.class.included_modules.include?(Mongoid::Timestamps::Created) or
|
101
|
+
doc.class.included_modules.include?(Mongoid::Timestamps::Updated)
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
105
|
+
chain :with do |timestamped_module|
|
106
|
+
@timestamped_module = timestamped_module
|
96
107
|
end
|
97
108
|
|
98
109
|
description do
|
99
|
-
"be a timestamped Mongoid document"
|
110
|
+
desc = "be a timestamped Mongoid document"
|
111
|
+
desc << " with #{@timestamped_module}" if @timestamped_module
|
112
|
+
desc
|
100
113
|
end
|
101
114
|
end
|
102
115
|
|
data/lib/matchers/validations.rb
CHANGED
@@ -1,20 +1,20 @@
|
|
1
1
|
module Mongoid
|
2
2
|
module Matchers
|
3
|
-
module Validations
|
4
|
-
|
3
|
+
module Validations
|
4
|
+
|
5
5
|
class HaveValidationMatcher
|
6
|
-
|
6
|
+
|
7
7
|
def initialize(field, validation_type)
|
8
8
|
@field = field.to_s
|
9
9
|
@type = validation_type.to_s
|
10
10
|
@options = {}
|
11
11
|
end
|
12
|
-
|
12
|
+
|
13
13
|
def matches?(actual)
|
14
14
|
@klass = actual.is_a?(Class) ? actual : actual.class
|
15
|
-
|
15
|
+
|
16
16
|
@validator = @klass.validators_on(@field).detect{|v| v.kind.to_s == @type}
|
17
|
-
|
17
|
+
|
18
18
|
if @validator
|
19
19
|
@negative_result_message = "#{@type.inspect} validator on #{@field.inspect}"
|
20
20
|
@positive_result_message = "#{@type.inspect} validator on #{@field.inspect}"
|
@@ -23,37 +23,44 @@ module Mongoid
|
|
23
23
|
return false
|
24
24
|
end
|
25
25
|
@result = true
|
26
|
-
check_on if @options[:on]
|
26
|
+
check_on if @options[:on]
|
27
27
|
@result
|
28
28
|
end
|
29
|
-
|
30
|
-
def failure_message_for_should
|
31
|
-
"Expected #{@klass.inspect} to #{description}; instead got #{@negative_result_message}"
|
29
|
+
|
30
|
+
def failure_message_for_should
|
31
|
+
"Expected #{@klass.inspect} to #{description}; instead got #{@negative_result_message}"
|
32
32
|
end
|
33
|
-
|
34
|
-
def failure_message_for_should_not
|
35
|
-
"Expected #{@klass.inspect} to not #{description}; instead got #{@positive_result_message}"
|
33
|
+
|
34
|
+
def failure_message_for_should_not
|
35
|
+
"Expected #{@klass.inspect} to not #{description}; instead got #{@positive_result_message}"
|
36
36
|
end
|
37
|
-
|
37
|
+
|
38
38
|
def description
|
39
|
-
"validate #{@type} of #{@field.inspect}"
|
39
|
+
desc = "validate #{@type} of #{@field.inspect}"
|
40
|
+
desc << " on #{@options[:on]}" if @options[:on]
|
41
|
+
desc
|
40
42
|
end
|
41
|
-
|
43
|
+
|
42
44
|
def on(*on_method)
|
43
45
|
@options[:on] = on_method.flatten
|
44
46
|
self
|
45
|
-
end
|
46
|
-
|
47
|
+
end
|
48
|
+
|
47
49
|
def check_on
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
@
|
50
|
+
validator_on_methods = [@validator.options[:on]].flatten
|
51
|
+
|
52
|
+
if validator_on_methods.any?
|
53
|
+
message = " on methods: #{validator_on_methods}"
|
54
|
+
|
55
|
+
if (@options[:on] - validator_on_methods).empty?
|
56
|
+
@positive_result_message << message
|
57
|
+
else
|
58
|
+
@negative_result_message << message
|
59
|
+
@result = false
|
60
|
+
end
|
54
61
|
end
|
55
62
|
end
|
56
|
-
end
|
63
|
+
end
|
57
64
|
end
|
58
65
|
end
|
59
66
|
end
|
data/spec/models/user.rb
CHANGED
@@ -1,11 +1,13 @@
|
|
1
1
|
class User
|
2
2
|
include Mongoid::Document
|
3
|
+
include Mongoid::Timestamps::Created
|
3
4
|
|
4
5
|
field :login
|
5
6
|
field :email
|
6
7
|
field :role
|
7
8
|
field :age, type: Integer
|
8
9
|
field :password, type: String
|
10
|
+
field :provider_uid
|
9
11
|
|
10
12
|
belongs_to :site, :inverse_of => :users
|
11
13
|
has_many :articles, :foreign_key => :author_id
|
@@ -20,7 +22,8 @@ class User
|
|
20
22
|
validates :role, :presence => true, :inclusion => { :in => ["admin", "moderator", "member"]}
|
21
23
|
validates :profile, :presence => true, :associated => true
|
22
24
|
validates :age, :presence => true, :numericality => true, :inclusion => { :in => 23..42 }, :on => [:create, :update]
|
23
|
-
validates :password, :presence => true, :on => :create
|
25
|
+
validates :password, :presence => true, :on => [:create, :update]
|
26
|
+
validates :provider_uid, presence: true
|
24
27
|
|
25
28
|
attr_accessible :login, :email, :age, :password
|
26
29
|
attr_accessible :role, :as => :admin
|
data/spec/unit/document_spec.rb
CHANGED
@@ -3,15 +3,15 @@ require 'spec_helper'
|
|
3
3
|
describe "Document" do
|
4
4
|
describe User do
|
5
5
|
it { should have_fields(:email, :login) }
|
6
|
+
it { should be_timestamped_document }
|
7
|
+
it { should be_timestamped_document.with(:created) }
|
8
|
+
it { should_not be_timestamped_document.with(:updated) }
|
6
9
|
end
|
7
|
-
|
10
|
+
|
8
11
|
describe Article do
|
9
12
|
it { should have_field(:published).of_type(Boolean).with_default_value_of(false) }
|
10
|
-
it { should have_field(:allow_comments).of_type(Boolean).with_default_value_of(true) }
|
13
|
+
it { should have_field(:allow_comments).of_type(Boolean).with_default_value_of(true) }
|
11
14
|
it { should_not have_field(:allow_comments).of_type(Boolean).with_default_value_of(false) }
|
12
|
-
end
|
13
|
-
|
14
|
-
describe Article do
|
15
15
|
it { should be_mongoid_document }
|
16
16
|
it { should be_versioned_document }
|
17
17
|
it { should be_timestamped_document }
|
@@ -19,6 +19,7 @@ describe "Validations" do
|
|
19
19
|
it { should validate_numericality_of(:age).on(:create, :update) }
|
20
20
|
it { should validate_inclusion_of(:age).to_allow(23..42).on([:create, :update]) }
|
21
21
|
it { should validate_presence_of(:password).on(:create) }
|
22
|
+
it { should validate_presence_of(:provider_uid).on(:create) }
|
22
23
|
end
|
23
24
|
|
24
25
|
describe Profile 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.5.
|
4
|
+
version: 1.5.1
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -13,7 +13,7 @@ date: 2012-07-27 00:00:00.000000000 Z
|
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rake
|
16
|
-
requirement: &
|
16
|
+
requirement: &70095909068120 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: '0'
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *70095909068120
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: mongoid
|
27
|
-
requirement: &
|
27
|
+
requirement: &70095909066740 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ! '>='
|
@@ -32,10 +32,10 @@ dependencies:
|
|
32
32
|
version: 3.0.1
|
33
33
|
type: :runtime
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *70095909066740
|
36
36
|
- !ruby/object:Gem::Dependency
|
37
37
|
name: rspec
|
38
|
-
requirement: &
|
38
|
+
requirement: &70095909065960 !ruby/object:Gem::Requirement
|
39
39
|
none: false
|
40
40
|
requirements:
|
41
41
|
- - ! '>='
|
@@ -43,7 +43,7 @@ dependencies:
|
|
43
43
|
version: '2.9'
|
44
44
|
type: :runtime
|
45
45
|
prerelease: false
|
46
|
-
version_requirements: *
|
46
|
+
version_requirements: *70095909065960
|
47
47
|
description: RSpec matches for Mongoid models, including association and validation
|
48
48
|
matchers
|
49
49
|
email: evansagge@gmail.com
|