README.md in mongoid-rspec-4.1.0 vs README.md in mongoid-rspec-4.2.0

- old
+ new

@@ -1,12 +1,19 @@ # [mongoid-rspec](https://github.com/mongoid/mongoid-rspec "A collection of RSpec-compatible matchers that help to test Mongoid documents.") -[![Build Status](https://travis-ci.org/mongoid/mongoid-rspec.svg?branch=master)](https://travis-ci.org/mongoid/mongoid-rspec) [![Gem Version](https://badge.fury.io/rb/mongoid-rspec.svg)](https://badge.fury.io/rb/mongoid-rspec) +[![Test Status](https://github.com/mongoid/mongoid-rspec/workflows/Test/badge.svg)](https://github.com/mongoid/mongoid-rspec/actions) +[![Rubocop Status](https://github.com/mongoid/mongoid-rspec/workflows/Rubocop/badge.svg)](https://github.com/mongoid/mongoid-rspec/actions) The mongoid-rspec library provides a collection of RSpec-compatible matchers that help to test Mongoid documents. +[Tested](https://github.com/mongoid/mongoid-locker/actions) against: +- MRI: `2.6.x`, `2.7.x`, `3.0.x`, `3.1.x`, `3.2.x` +- Mongoid: `4`, `5`, `6`, `7`, `8`, `9` + +See [.github/workflows/rspec.yml](.github/workflows/rspec.yml) for the latest test matrix. + ## Installation Drop this line into your Gemfile: ```ruby @@ -16,11 +23,11 @@ ``` ## Compatibility -This gem is compatible with Mongoid 3, 4, 5, 6 and 7. +This gem is compatible with Mongoid 3, 4, 5, 6, 7, 8, 9. ## Configuration ### Rails @@ -179,33 +186,33 @@ ### have_index_for ```ruby class Article - index({ title: 1 }, { unique: true, background: true, drop_dups: true }) + index({ title: 1 }, { unique: true, background: true }) index({ title: 1, created_at: -1 }) index({ category: 1 }) end RSpec.describe Article, type: :model do it do is_expected .to have_index_for(title: 1) - .with_options(unique: true, background: true, drop_dups: true) + .with_options(unique: true, background: true) end it { is_expected.to have_index_for(title: 1, created_at: -1) } it { is_expected.to have_index_for(category: 1) } end ``` ### Field Matchers ```ruby RSpec.describe Article do - it { is_expected.to have_field(:published).of_type(Boolean).with_default_value_of(false) } - it { is_expected.to have_field(:allow_comments).of_type(Boolean).with_default_value_of(true) } - it { is_expected.not_to have_field(:allow_comments).of_type(Boolean).with_default_value_of(false) } + it { is_expected.to have_field(:published).of_type(Mongoid::Boolean).with_default_value_of(false) } + it { is_expected.to have_field(:allow_comments).of_type(Mongoid::Boolean).with_default_value_of(true) } + it { is_expected.not_to have_field(:allow_comments).of_type(Mongoid::Boolean).with_default_value_of(false) } it { is_expected.not_to have_field(:number_of_comments).of_type(Integer).with_default_value_of(1) } end RSpec.describe User do it { is_expected.to have_fields(:email, :login) } @@ -247,10 +254,31 @@ it { is_expected.to belong_to(:author).of_type(User).as_inverse_of(:articles) } it { is_expected.to belong_to(:author).of_type(User).as_inverse_of(:articles).with_index } it { is_expected.to embed_many(:comments) } end +# when the touch option is provided, then we can also verify that it is set + +# by default, with_touch matches true when no parameters are provided +describe Article do + it { is_expected.to belong_to(:author).of_type(User).as_inverse_of(:articles).with_index.with_touch } +end + +# parameters are supported for explicit matching +describe Comment do + it { is_expected.to be_embedded_in(:article).as_inverse_of(:comments).with_polymorphism.with_touch(true) } +end + +describe Permalink do + it { is_expected.to be_embedded_in(:linkable).as_inverse_of(:link).with_touch(false) } +end + +# touch can also take a symbol representing a field on the parent to touch +describe Record do + it { is_expected.to belong_to(:user).as_inverse_of(:record).with_touch(:record_updated_at) } +end + RSpec.describe Comment do it { is_expected.to be_embedded_in(:article).as_inverse_of(:comments) } it { is_expected.to belong_to(:user).as_inverse_of(:comments) } end @@ -312,9 +340,25 @@ RSpec.describe Person do # in order to be able to use the custom_validate matcher, the custom validator class (in this case SsnValidator) # should redefine the kind method to return :custom, i.e. "def self.kind() :custom end" it { is_expected.to custom_validate(:ssn).with_validator(SsnValidator) } +end + +# If you're using validators with if/unless conditionals, spec subject must be object instance +# This is supported on Mongoid 4 and newer +Rspec.describe User do + context 'when user has `admin` role' do + subject { User.new(role: 'admin') } + + it { is_expected.to validate_length_of(:password).greater_than(20) } + end + + context 'when user does not have `admin` role' do + subject { User.new(role: 'member') } + + it { is_expected.not_to validate_length_of(:password) } + end end ``` ### Mass Assignment Matcher