mongoid-rspec 3.0.0 → 4.1.0

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.
Files changed (64) hide show
  1. checksums.yaml +5 -5
  2. data/LICENSE +1 -1
  3. data/README.md +220 -109
  4. data/Rakefile +8 -5
  5. data/lib/matchers/accept_nested_attributes.rb +22 -23
  6. data/lib/matchers/allow_mass_assignment.rb +12 -12
  7. data/lib/matchers/associations.rb +85 -49
  8. data/lib/matchers/be_dynamic_document.rb +26 -0
  9. data/lib/matchers/be_mongoid_document.rb +26 -0
  10. data/lib/matchers/be_stored_in.rb +55 -0
  11. data/lib/matchers/have_field.rb +90 -0
  12. data/lib/matchers/have_timestamps.rb +61 -0
  13. data/lib/matchers/indexes/have_index_for.rb +16 -0
  14. data/lib/matchers/indexes/v3/have_index_for.rb +59 -0
  15. data/lib/matchers/indexes/v4/have_index_for.rb +54 -0
  16. data/lib/matchers/validations/absence_of.rb +11 -0
  17. data/lib/matchers/validations/associated.rb +1 -1
  18. data/lib/matchers/validations/confirmation_of.rb +0 -7
  19. data/lib/matchers/validations/custom_validation_of.rb +1 -4
  20. data/lib/matchers/validations/exclusion_of.rb +5 -5
  21. data/lib/matchers/validations/format_of.rb +2 -2
  22. data/lib/matchers/validations/inclusion_of.rb +5 -5
  23. data/lib/matchers/validations/length_of.rb +13 -35
  24. data/lib/matchers/validations/numericality_of.rb +32 -16
  25. data/lib/matchers/validations/presence_of.rb +1 -1
  26. data/lib/matchers/validations/uniqueness_of.rb +7 -10
  27. data/lib/matchers/validations.rb +30 -11
  28. data/lib/mongoid/rspec/version.rb +2 -2
  29. data/lib/mongoid/rspec.rb +18 -5
  30. data/spec/models/article.rb +9 -6
  31. data/spec/models/comment.rb +1 -1
  32. data/spec/models/log.rb +3 -3
  33. data/spec/models/message.rb +17 -0
  34. data/spec/models/movie_article.rb +1 -2
  35. data/spec/models/person.rb +1 -1
  36. data/spec/models/profile.rb +2 -2
  37. data/spec/models/record.rb +1 -1
  38. data/spec/models/site.rb +5 -1
  39. data/spec/models/user.rb +12 -11
  40. data/spec/spec_helper.rb +9 -10
  41. data/spec/unit/accept_nested_attributes_spec.rb +1 -1
  42. data/spec/unit/associations_spec.rb +19 -7
  43. data/spec/unit/be_dynamic_document_spec.rb +21 -0
  44. data/spec/unit/be_mongoid_document_spec.rb +25 -0
  45. data/spec/unit/be_stored_in.rb +54 -0
  46. data/spec/unit/document_spec.rb +5 -14
  47. data/spec/unit/have_index_for_spec.rb +46 -0
  48. data/spec/unit/have_timestamps_spec.rb +71 -0
  49. data/spec/unit/validations_spec.rb +23 -15
  50. data/spec/validators/ssn_validator.rb +6 -6
  51. metadata +115 -39
  52. data/.document +0 -5
  53. data/.gitignore +0 -6
  54. data/.ruby-gemset +0 -1
  55. data/.ruby-version +0 -1
  56. data/.travis.yml +0 -10
  57. data/Gemfile +0 -4
  58. data/lib/matchers/collections.rb +0 -9
  59. data/lib/matchers/document.rb +0 -173
  60. data/lib/matchers/indexes.rb +0 -69
  61. data/lib/matchers/validations/with_message.rb +0 -27
  62. data/mongoid-rspec.gemspec +0 -26
  63. data/spec/unit/collections_spec.rb +0 -7
  64. data/spec/unit/indexes_spec.rb +0 -17
data/spec/models/site.rb CHANGED
@@ -3,7 +3,11 @@ class Site
3
3
 
4
4
  field :name
5
5
 
6
- has_many :users, inverse_of: :site, order: :email.desc, counter_cache: true
6
+ if Mongoid::Compatibility::Version.mongoid6_or_older?
7
+ has_many :users, inverse_of: :site, order: :email.desc, counter_cache: true
8
+ else
9
+ has_many :users, inverse_of: :site, order: :email.desc
10
+ end
7
11
 
8
12
  validates :name, presence: true, uniqueness: true
9
13
  end
data/spec/models/user.rb CHANGED
@@ -13,21 +13,22 @@ class User
13
13
  belongs_to :site, inverse_of: :users
14
14
  has_many :articles, foreign_key: :author_id, order: :title
15
15
  has_many :comments, dependent: :destroy, autosave: true
16
- has_and_belongs_to_many :children, class_name: "User"
17
- has_one :record, autobuild: true
16
+ has_many :messages, dependent: :destroy
17
+ has_and_belongs_to_many :children, class_name: 'User'
18
+ has_one :record, autobuild: true, inverse_of: :user
18
19
 
19
- embeds_one :profile
20
+ embeds_one :profile, inverse_of: :user
20
21
 
21
- validates :login, presence: true, uniqueness: { scope: :site }, format: { with: /\A[\w\-]+\z/ }, exclusion: { in: ["super", "index", "edit"] }
22
- validates :email, uniqueness: { case_sensitive: false, scope: :site, message: "is already taken" }, confirmation: true
23
- validates :role, presence: true, inclusion: { in: ["admin", "moderator", "member"] }
22
+ validates :login, presence: true, uniqueness: { scope: :site }, format: { with: /\A[\w\-]+\z/ }, exclusion: { in: %w[super index edit] }
23
+ validates :email, uniqueness: { case_sensitive: false, scope: :site, message: 'is already taken' }, confirmation: true
24
+ validates :role, presence: true, inclusion: { in: %w[admin moderator member] }
24
25
  validates :profile, presence: true, associated: true
25
- validates :age, presence: true, numericality: true, inclusion: { in: 23..42 }, on: [:create, :update]
26
- validates :password, presence: true, on: [:create, :update]
27
- validates :password, exclusion: { in: ->(user) { ['password'] } }
28
- validates :password, confirmation: { message: "Password confirmation must match given password" }
26
+ validates :age, presence: true, numericality: true, inclusion: { in: 23..42 }, on: %i[create update]
27
+ validates :password, presence: true, on: %i[create update]
28
+ validates :password, exclusion: { in: ->(_user) { ['password'] } }
29
+ validates :password, confirmation: { message: 'Password confirmation must match given password' }
29
30
  validates :provider_uid, presence: true
30
- validates :locale, inclusion: { in: ->(user) { [:en, :ru] } }
31
+ validates :locale, inclusion: { in: ->(_user) { %i[en ru] } }
31
32
 
32
33
  accepts_nested_attributes_for :articles, :comments
33
34
 
data/spec/spec_helper.rb CHANGED
@@ -1,24 +1,23 @@
1
1
  $LOAD_PATH.unshift(File.dirname(__FILE__))
2
- $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), "lib"))
3
- MODELS = File.join(File.dirname(__FILE__), "models")
2
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), 'lib'))
3
+ MODELS = File.join(File.dirname(__FILE__), 'models')
4
4
  $LOAD_PATH.unshift(MODELS)
5
- VALIDATORS = File.join(File.dirname(__FILE__), "validators")
5
+ VALIDATORS = File.join(File.dirname(__FILE__), 'validators')
6
6
  $LOAD_PATH.unshift(VALIDATORS)
7
7
 
8
- require "rubygems"
9
- require "bundler"
8
+ require 'rubygems'
9
+ require 'bundler'
10
10
  Bundler.setup
11
11
 
12
12
  require 'mongoid'
13
- require 'rspec'
14
13
  require 'rspec/core'
15
14
  require 'rspec/expectations'
15
+ require 'mongoid/compatibility'
16
16
 
17
- Mongoid.configure do |config|
18
- config.connect_to("mongoid-rspec-test")
19
- end
17
+ Mongoid::Config.connect_to('mongoid-rspec-test') if Mongoid::Compatibility::Version.mongoid3_or_newer?
18
+ Mongo::Logger.logger.level = ::Logger::INFO if Mongoid::Compatibility::Version.mongoid5_or_newer?
20
19
 
21
- Dir[ File.join(MODELS, "*.rb") ].sort.each { |file| require File.basename(file) }
20
+ Dir[File.join(MODELS, '*.rb')].sort.each { |file| require File.basename(file) }
22
21
 
23
22
  require 'mongoid-rspec'
24
23
 
@@ -1,6 +1,6 @@
1
1
  require 'spec_helper'
2
2
 
3
- RSpec.describe "AcceptsNestedAttributes" do
3
+ RSpec.describe 'AcceptsNestedAttributes' do
4
4
  describe User do
5
5
  it { is_expected.to accept_nested_attributes_for(:articles) }
6
6
  it { is_expected.to accept_nested_attributes_for(:comments) }
@@ -1,14 +1,16 @@
1
1
  require 'spec_helper'
2
2
 
3
- RSpec.describe "Associations" do
3
+ RSpec.describe 'Associations' do
4
4
  describe User do
5
- it { is_expected.to have_many(:articles).with_foreign_key(:author_id).ordered_by(:title) }
5
+ if Mongoid::Compatibility::Version.mongoid6_or_newer?
6
+ it { is_expected.to have_many(:articles).with_foreign_key(:author_id).ordered_by(:title) }
7
+ end
6
8
 
7
- it { is_expected.to have_one(:record).with_autobuild }
9
+ it { is_expected.to have_one(:record).as_inverse_of(:user).with_autobuild }
8
10
 
9
11
  it { is_expected.to have_many(:comments).with_dependent(:destroy).with_autosave }
10
12
 
11
- it { is_expected.to embed_one(:profile) }
13
+ it { is_expected.to embed_one(:profile).as_inverse_of(:user) }
12
14
 
13
15
  it { is_expected.to have_and_belong_to_many(:children).of_type(User) }
14
16
  end
@@ -19,8 +21,8 @@ RSpec.describe "Associations" do
19
21
 
20
22
  describe Article do
21
23
  it { is_expected.to belong_to(:author).of_type(User).as_inverse_of(:articles).with_index }
22
- it { is_expected.to embed_many(:comments).with_cascading_callbacks }
23
- it { is_expected.to embed_one(:permalink) }
24
+ it { is_expected.to embed_many(:comments).as_inverse_of(:article).with_cascading_callbacks }
25
+ it { is_expected.to embed_one(:permalink).as_inverse_of(:linkable) }
24
26
  end
25
27
 
26
28
  describe Comment do
@@ -37,6 +39,16 @@ RSpec.describe "Associations" do
37
39
  end
38
40
 
39
41
  describe Site do
40
- it { is_expected.to have_many(:users).as_inverse_of(:site).ordered_by(:email.desc).with_counter_cache }
42
+ if Mongoid::Compatibility::Version.mongoid6_or_older?
43
+ it { is_expected.to have_many(:users).as_inverse_of(:site).ordered_by(:email.desc).with_counter_cache }
44
+ elsif Mongoid::Compatibility::Version.mongoid7_or_newer?
45
+ it { is_expected.to have_many(:users).as_inverse_of(:site).ordered_by(:email.desc) }
46
+ end
47
+ end
48
+
49
+ describe Message do
50
+ if Mongoid::Compatibility::Version.mongoid6_or_newer?
51
+ it { is_expected.to belong_to(:user).with_optional }
52
+ end
41
53
  end
42
54
  end
@@ -0,0 +1,21 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe Mongoid::Matchers::BeDynamicDocument do
4
+ context 'when model does\'t include Mongoid::Document' do
5
+ subject do
6
+ Class.new
7
+ end
8
+
9
+ it { is_expected.not_to be_mongoid_document }
10
+ end
11
+
12
+ context 'when model doesn\'t include Mongoid::Document' do
13
+ subject do
14
+ Class.new do
15
+ include Mongoid::Document
16
+ end
17
+ end
18
+
19
+ it { is_expected.to be_mongoid_document }
20
+ end
21
+ end
@@ -0,0 +1,25 @@
1
+ require 'spec_helper'
2
+ if Mongoid::Compatibility::Version.mongoid4_or_newer?
3
+ RSpec.describe Mongoid::Matchers::BeMongoidDocument do
4
+ context 'when model does\'t include Mongoid::Attributes::Dynamic' do
5
+ subject do
6
+ Class.new do
7
+ include Mongoid::Document
8
+ end
9
+ end
10
+
11
+ it { is_expected.not_to be_dynamic_document }
12
+ end
13
+
14
+ context 'when model doesn\'t include Mongoid::Attributes::Dynamic' do
15
+ subject do
16
+ Class.new do
17
+ include Mongoid::Document
18
+ include Mongoid::Attributes::Dynamic
19
+ end
20
+ end
21
+
22
+ it { is_expected.to be_dynamic_document }
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,54 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe Mongoid::Matchers::BeStoredIn do
4
+ subject do
5
+ Class.new do
6
+ include Mongoid::Document
7
+ store_in collection: 'citizens', database: 'other', client: 'secondary'
8
+ end
9
+ end
10
+
11
+ it 'detects storage options' do
12
+ is_expected.to be_stored_in(collection: 'citizens', database: 'other', client: 'secondary')
13
+ end
14
+
15
+ it 'detects even part of storage options' do
16
+ is_expected.to be_stored_in(database: 'other')
17
+ is_expected.to be_stored_in(client: 'secondary')
18
+ is_expected.to be_stored_in(collection: 'citizens')
19
+ is_expected.to be_stored_in(collection: 'citizens', database: 'other')
20
+ is_expected.to be_stored_in(database: 'other', client: 'secondary')
21
+ is_expected.to be_stored_in(collection: 'citizens', client: 'secondary')
22
+ end
23
+
24
+ it 'detects differences' do
25
+ is_expected.not_to be_stored_in(collection: 'aliens')
26
+ end
27
+
28
+ context 'when models has storage options defined via blocks, procs or lambdas' do
29
+ subject do
30
+ Class.new do
31
+ include Mongoid::Document
32
+ store_in database: -> { Thread.current[:database] }
33
+ end
34
+ end
35
+
36
+ before do
37
+ Thread.current[:database] = 'db1981'
38
+ end
39
+
40
+ it 'detects storage options' do
41
+ is_expected.to be_stored_in(database: 'db1981')
42
+ end
43
+
44
+ it 'reflects changes in storage options' do
45
+ is_expected.to be_stored_in(database: 'db1981')
46
+ Thread.current[:database] = 'db2200'
47
+ is_expected.to be_stored_in(database: 'db2200')
48
+ end
49
+
50
+ it 'detects differences' do
51
+ is_expected.not_to be_stored_in(database: 'other')
52
+ end
53
+ end
54
+ end
@@ -1,26 +1,17 @@
1
1
  require 'spec_helper'
2
2
 
3
- RSpec.describe "Document" do
3
+ RSpec.describe 'Document' do
4
4
  describe User do
5
5
  it { is_expected.to have_fields(:email, :login) }
6
- it { is_expected.to be_timestamped_document }
7
- it { is_expected.to be_timestamped_document.with(:created) }
8
- it { is_expected.not_to be_timestamped_document.with(:updated) }
9
6
  end
10
7
 
11
8
  describe Article do
12
- it { is_expected.to have_field(:published).of_type(Mongoid::Boolean).with_default_value_of(false) }
13
- it { is_expected.to have_field(:allow_comments).of_type(Mongoid::Boolean).with_default_value_of(true) }
9
+ klass_boolean = Mongoid::Compatibility::Version.mongoid4_or_newer? ? Mongoid::Boolean : Boolean
10
+ it { is_expected.to have_field(:published).of_type(klass_boolean).with_default_value_of(false) }
11
+ it { is_expected.to have_field(:allow_comments).of_type(klass_boolean).with_default_value_of(true) }
14
12
  it { is_expected.to belong_to(:author) }
15
13
  it { is_expected.to have_field(:title).localized }
16
- it { is_expected.not_to have_field(:allow_comments).of_type(Mongoid::Boolean).with_default_value_of(false) }
14
+ it { is_expected.not_to have_field(:allow_comments).of_type(klass_boolean).with_default_value_of(false) }
17
15
  it { is_expected.not_to have_field(:number_of_comments).of_type(Integer).with_default_value_of(1) }
18
- it { is_expected.to be_mongoid_document }
19
- it { is_expected.to be_timestamped_document }
20
- end
21
-
22
- describe Log do
23
- it { is_expected.to be_mongoid_document }
24
- it { is_expected.to be_dynamic_document }
25
16
  end
26
17
  end
@@ -0,0 +1,46 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe Mongoid::Matchers::HaveIndexFor do
4
+ subject do
5
+ Class.new do
6
+ include Mongoid::Document
7
+
8
+ field :fizz, as: :buzz, type: String
9
+
10
+ index(foo: 1)
11
+ index({ bar: 1 }, unique: true, background: true, drop_dups: true)
12
+ index(foo: 1, bar: -1)
13
+ index('baz._id' => 1)
14
+ index(buzz: 1)
15
+ end
16
+ end
17
+
18
+ it 'detects an index for singular field key' do
19
+ is_expected.to have_index_for(foo: 1)
20
+ end
21
+
22
+ it 'detects an index for multipple fields key' do
23
+ is_expected.to have_index_for(foo: 1, bar: -1)
24
+ end
25
+
26
+ it 'detects an index with options' do
27
+ is_expected
28
+ .to have_index_for(bar: 1)
29
+ .with_options(unique: true, background: true, drop_dups: true)
30
+ end
31
+
32
+ it 'detects an index with only part of options' do
33
+ is_expected
34
+ .to have_index_for(bar: 1)
35
+ .with_options(unique: true)
36
+ end
37
+
38
+ it 'detects an index for string key' do
39
+ is_expected.to have_index_for('baz._id' => 1)
40
+ end
41
+
42
+ it 'detect an index for aliased fields' do
43
+ is_expected.to have_index_for(fizz: 1)
44
+ is_expected.to have_index_for(buzz: 1) if Mongoid::Compatibility::Version.mongoid4_or_newer?
45
+ end
46
+ end
@@ -0,0 +1,71 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe Mongoid::Matchers::HaveTimestamps do
4
+ context 'when model includes Mongoid::Timestamps' do
5
+ subject do
6
+ Class.new do
7
+ include Mongoid::Document
8
+ include Mongoid::Timestamps
9
+ end
10
+ end
11
+
12
+ it { is_expected.to have_timestamps }
13
+ end
14
+
15
+ context 'when model includes Mongoid::Timestamps::Short' do
16
+ subject do
17
+ Class.new do
18
+ include Mongoid::Document
19
+ include Mongoid::Timestamps::Short
20
+ end
21
+ end
22
+
23
+ it { is_expected.to have_timestamps.shortened }
24
+ end
25
+
26
+ context 'when model includes Mongoid::Timestamps::Updated' do
27
+ subject do
28
+ Class.new do
29
+ include Mongoid::Document
30
+ include Mongoid::Timestamps::Updated
31
+ end
32
+ end
33
+
34
+ it { is_expected.to have_timestamps.for(:updating) }
35
+ end
36
+
37
+ context 'when model includes Mongoid::Timestamps::Updated::Short' do
38
+ subject do
39
+ Class.new do
40
+ include Mongoid::Document
41
+ include Mongoid::Timestamps::Updated::Short
42
+ end
43
+ end
44
+
45
+ it { is_expected.to have_timestamps.for(:updating).shortened }
46
+ it { is_expected.to have_timestamps.shortened.for(:updating) }
47
+ end
48
+
49
+ context 'when model includes Mongoid::Timestamps::Created' do
50
+ subject do
51
+ Class.new do
52
+ include Mongoid::Document
53
+ include Mongoid::Timestamps::Created
54
+ end
55
+ end
56
+
57
+ it { is_expected.to have_timestamps.for(:creating) }
58
+ end
59
+
60
+ context 'when model includes Mongoid::Timestamps::Created::Short' do
61
+ subject do
62
+ Class.new do
63
+ include Mongoid::Document
64
+ include Mongoid::Timestamps::Created::Short
65
+ end
66
+ end
67
+
68
+ it { is_expected.to have_timestamps.for(:creating).shortened }
69
+ it { is_expected.to have_timestamps.shortened.for(:creating) }
70
+ end
71
+ end
@@ -1,6 +1,6 @@
1
1
  require 'spec_helper'
2
2
 
3
- RSpec.describe "Validations" do
3
+ RSpec.describe 'Validations' do
4
4
  describe Site do
5
5
  it { is_expected.to validate_presence_of(:name) }
6
6
  it { is_expected.to validate_uniqueness_of(:name) }
@@ -9,45 +9,53 @@ RSpec.describe "Validations" do
9
9
  describe User do
10
10
  it { is_expected.to validate_presence_of(:login) }
11
11
  it { is_expected.to validate_uniqueness_of(:login).scoped_to(:site) }
12
- it { is_expected.to validate_uniqueness_of(:email).case_insensitive.with_message("is already taken") }
13
- it { is_expected.to validate_format_of(:login).to_allow("valid_login").not_to_allow("invalid login") }
12
+ it { is_expected.to validate_uniqueness_of(:email).case_insensitive.with_message('is already taken') }
13
+ it { is_expected.to validate_format_of(:login).to_allow('valid_login').not_to_allow('invalid login') }
14
14
  it { is_expected.to validate_associated(:profile) }
15
- it { is_expected.to validate_exclusion_of(:login).to_not_allow("super", "index", "edit") }
16
- it { is_expected.to validate_exclusion_of(:password).to_not_allow("password") }
17
- it { is_expected.to validate_inclusion_of(:role).to_allow("admin", "member") }
18
- it { is_expected.to validate_inclusion_of(:role).to_allow(["admin", "member"]) }
15
+ it { is_expected.to validate_exclusion_of(:login).to_not_allow('super', 'index', 'edit') }
16
+ it { is_expected.to validate_exclusion_of(:password).to_not_allow('password') }
17
+ it { is_expected.to validate_inclusion_of(:role).to_allow('admin', 'member') }
18
+ it { is_expected.to validate_inclusion_of(:role).to_allow(%w[admin member]) }
19
19
  it { is_expected.to validate_confirmation_of(:email) }
20
20
  it { is_expected.to validate_presence_of(:age).on(:create, :update) }
21
21
  it { is_expected.to validate_numericality_of(:age).on(:create, :update) }
22
- it { is_expected.to validate_inclusion_of(:age).to_allow(23..42).on([:create, :update]) }
22
+ it { is_expected.to validate_inclusion_of(:age).to_allow(23..42).on(%i[create update]) }
23
23
  it { is_expected.to validate_presence_of(:password).on(:create) }
24
- it { is_expected.to validate_confirmation_of(:password).with_message("Password confirmation must match given password") }
24
+ it { is_expected.to validate_confirmation_of(:password).with_message('Password confirmation must match given password') }
25
25
  it { is_expected.to validate_presence_of(:provider_uid).on(:create) }
26
- it { is_expected.to validate_inclusion_of(:locale).to_allow([:en, :ru]) }
26
+ it { is_expected.to validate_inclusion_of(:locale).to_allow(%i[en ru]) }
27
27
  end
28
28
 
29
29
  describe Profile do
30
30
  it { is_expected.to validate_numericality_of(:age).greater_than(0) }
31
+ it { is_expected.not_to validate_numericality_of(:age).greater_than(0).only_integer(true) }
31
32
  it { is_expected.to validate_acceptance_of(:terms_of_service) }
32
- it { is_expected.to validate_length_of(:hobbies).with_minimum(1).with_message("requires at least one hobby") }
33
+ it { is_expected.to validate_length_of(:hobbies).with_minimum(1).with_message('requires at least one hobby') }
33
34
  end
34
35
 
35
36
  describe Article do
36
37
  it { is_expected.to validate_length_of(:title).within(8..16) }
37
38
  it { is_expected.not_to validate_length_of(:content).greater_than(200).less_than(16) }
38
39
  it { is_expected.to validate_length_of(:content).greater_than(200) }
39
- it { is_expected.to validate_inclusion_of(:status).to_allow([:pending]).on( :create ) }
40
- it { is_expected.to validate_inclusion_of(:status).to_allow([:approved, :rejected]).on( :update ) }
40
+ it { is_expected.to validate_inclusion_of(:status).to_allow([:pending]).on(:create) }
41
+ it { is_expected.to validate_inclusion_of(:status).to_allow(%i[approved rejected]).on(:update) }
42
+ it { is_expected.to validate_absence_of(:deletion_date) } if Mongoid::Compatibility::Version.mongoid4_or_newer?
41
43
  end
42
44
 
43
45
  describe MovieArticle do
44
46
  it { is_expected.to validate_numericality_of(:rating).greater_than(0) }
45
- it { is_expected.to validate_numericality_of(:rating).to_allow(:greater_than => 0).less_than_or_equal_to(5) }
46
- it { is_expected.to validate_numericality_of(:classification).to_allow(:even => true, :only_integer => true, :nil => false) }
47
+ it { is_expected.to validate_numericality_of(:rating).to_allow(greater_than: 0).less_than_or_equal_to(5) }
48
+ it { is_expected.to validate_numericality_of(:classification).to_allow(even: true, only_integer: true, nil: false) }
47
49
  end
48
50
 
49
51
  describe Person do
50
52
  it { is_expected.to custom_validate(:ssn).with_validator(SsnValidator) }
51
53
  it { is_expected.not_to custom_validate(:name) }
52
54
  end
55
+
56
+ describe Message do
57
+ it { is_expected.to validate_uniqueness_of(:identifier).with_message('uniqueness') }
58
+ it { is_expected.to validate_presence_of(:from).with_message('required') }
59
+ it { is_expected.to validate_format_of(:to).with_message('format') }
60
+ end
53
61
  end
@@ -1,16 +1,16 @@
1
- class SsnValidator < ActiveModel::EachValidator
2
-
1
+ class SsnValidator < ActiveModel::EachValidator
3
2
  def validate_each(record, attribute, value)
4
3
  unless valid_ssn?(record, attribute, value)
5
4
  record.errors[attribute] << "#{value} is not a valid Social Security Number"
6
5
  end
7
6
  end
8
7
 
9
- def self.kind() :custom end
8
+ def self.kind
9
+ :custom
10
+ end
10
11
 
11
- def valid_ssn?(record, attribute, value)
12
+ def valid_ssn?(_record, _attribute, _value)
12
13
  # irrelevant here how validation is done
13
14
  true
14
15
  end
15
-
16
- end
16
+ end