用于 Mongoid 的 RSpec 匹配器和宏。
The mongoid-rspec library provides a collection of RSpec-compatible matchers that help to test Mongoid documents.
Tested against:
2.6.x, 2.7.x, 3.0.x, 3.1.x, 3.2.x4, 5, 6, 7, 8, 9See .github/workflows/rspec.yml for the latest test matrix.
Drop this line into your Gemfile:
group :test do
gem 'mongoid-rspec'
end
This gem is compatible with Mongoid 3, 4, 5, 6, 7, 8, 9.
Add to your rails_helper.rb file
require 'mongoid-rspec'
RSpec.configure do |config|
config.include Mongoid::Matchers, type: :model
end
Add to your spec_helper.rb file.
require 'mongoid-rspec'
RSpec.configure do |config|
config.include Mongoid::Matchers
end
class Post
include Mongoid::Document
end
RSpec.describe Post, type: :model do
it { is_expected.to be_mongoid_document }
end
class User
include Mongoid::Document
include Mongoid::Attributes::Dynamic
end
RSpec.describe User, type: :model do
it { is_expected.to be_dynamic_document }
end
With full timestamps.
class Log
include Mongoid::Document
include Mongoid::Timestamps
end
RSpec.describe Log, type: :model do
it { is_expected.to have_timestamps }
end
With short timestamps.
class User
include Mongoid::Document
include Mongoid::Timestamps::Short
end
RSpec.describe User, type: :model do
it { is_expected.to have_timestamps.shortened }
end
With only creating or updating timestamps.
class Admin
include Mongoid::Document
include Mongoid::Timestamps::Create
include Mongoid::Timestamps::Update
end
RSpec.describe Admin, type: :model do
it { is_expected.to have_timestamps.for(:creating) }
it { is_expected.to have_timestamps.for(:updating) }
end
With short creating or updating timestamps.
class Post
include Mongoid::Document
include Mongoid::Timestamps::Create::Short
end
RSpec.describe Short, type: :model do
it { is_expected.to have_timestamps.for(:creating).shortened }
end
class Post
include Mongoid::Document
store_in database: 'db1', collection: 'messages', client: 'secondary'
end
RSpec.describe Post, type: :model do
it { is_expected.to be_stored_in(database: 'db1', collection: 'messages', client: 'secondary') }
end
It checks only those options, that you specify. For instance, test in example below will pass, even though expectation contains only database option.
class Comment
include Mongoid::Document
store_in database: 'db2', collection: 'messages'
end
RSpec.describe Comment, type: :model do
it { is_expected.to be_stored_in(database: 'db2') }
end
It works fine with lambdas and procs.
class User
include Mongoid::Document
store_in database: ->{ Thread.current[:database] }
end
RSpec.describe Post, type: :model do
it do
Thread.current[:database] = 'db3'
is_expected.to be_stored_in(database: 'db3')
Thread.current[:database] = 'db4'
is_expected.to be_stored_in(database: 'db4')
end
end
class Article
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)
end
it { is_expected.to have_index_for(title: 1, created_at: -1) }
it { is_expected.to have_index_for(category: 1) }
end
…
…
…
RSpec.describe User do
it { is_expected.to allow_mass_assignment_of(:login) }
it { is_expected.to allow_mass_assignment_of(:email) }
it { is_expected.to allow_mass_assignment_of(:age) }
it { is_expected.to allow_mass_assignment_of(:password) }
it { is_expected.to allow_mass_assignment_of(:password) }
it { is_expected.to allow_mass_assignment_of(:role).as(:admin) }
it { is_expected.not_to allow_mass_assignment_of(:role) }
end
RSpec.describe User do
it { is_expected.to accept_nested_attributes_for(:articles) }
it { is_expected.to accept_nested_attributes_for(:comments) }
end
RSpec.describe Article do
it { is_expected.to accept_nested_attributes_for(:permalink) }
end
You're encouraged to contribute to this library. See CONTRIBUTING for details.
Copyright (c) 2009-2018 Evan Sagge and Contributors.
MIT License. See LICENSE for details.
暂无开放 Issues,或尚未同步最近议题。