For detailed documentation, you can click the official instructions on GitHub, which is only an English version.
Https://github.com/thoughtbot/shoulda-matchers
Shoulda-matchers provides test: Unit-And rspec-compatible one-liners that test common rails functionality. These tests wowould otherwise be much longer, more complex, and error-prone.
Shoulda-matchers corresponds to the Method for Testing common rails functions, making the test cases very concise and efficient.
How to install and use it?
(1) Add the following lines to the gemfile file.
GROUP: test do gem 'shoulda-matchers', require: false end where require: false indicates not to automatically load
(2) Modify spec_helper.rb and add the following two lines.
Require 'rspec/rails 'require 'shoulda/matchers'
Next let's take a look at the Matching content.
Activemodel matchers
- Allow_mass_assignment_ofTests usage of rails 3's
attr_accessible
Andattr_protected
Macros.
- Allow_valueTests usage of
validates_format_of
Validation.
- Validate_inclusion_ofTests usage
validates_inclusion_of
.
- Validate_exclusion_ofTests usage
validates_exclusion_of
.
- Ensure_length_ofTests usage
validates_length_of
.
- Have_secure_passwordTests usage
has_secure_password
.
- Validate_confirmation_ofTests usage
validates_confirmation_of
.
- Validate_numericality_ofTests usage
validates_numericality_of
.
- Validate_presence_ofTests usage
validates_presence_of
.
- Validate_uniqueness_ofTests usage
validates_uniqueness_of
.
Activerecord matchers
- Accept_nested_attributes_forTests usage of
accepts_nested_attributes_for
Macro.
- Belong_toTests your
belongs_to
Associations.
- Define_enum_forTests usage of
enum
Macro.
- Have_tablesTests your
has_many
Associations.
- Have_oneTests your
has_one
Associations.
- Have_and_belong_to_propTests your
has_and_belongs_to_many
Associations.
- Have_db_columnTests that the table that backs your model has a specific column.
- Have_db_indexTests that the table that backs your model has an index on a specific column.
- Have_readonly_attributeTests usage of
attr_readonly
Macro.
- SerializeTests usage of
serialize
Macro.
Actioncontroller matchers
- Filter_paramTests parameter filtering configuration.
- Redirect_toTests that an action redirects to a certain location.
- Render_templateTests that an action renders a template.
- Render_with_layoutTests that an action is rendered with a certain layout.
- Rescue_fromTests usage of
rescue_from
Macro.
- Respond_withTests that an action responds with a certain status code.
- RouteTests your routes.
- Set_sessionMakes assertions on
session
Hash.
- Set_the_flashMakes assertions on
flash
Hash.
The following is an example.
# -*- encoding : utf-8 -*-class CmsUser < ActiveRecord::Base validates_presence_of :email validates_uniqueness_of :email def password=(password) write_attribute :password, self.class.md5(password) end def self.authenticate(username, password) CmsUser.where(username: username, password: md5(password)).first end def self.md5(str) Digest::MD5.hexdigest(str.to_s) endend
Check the corresponding cms_user_spec.rb test.
# -*- encoding : utf-8 -*-require ‘spec_helper‘describe CmsUser do describe "validations" do it { should validate_presence_of(:email) } it { should validate_uniqueness_of(:email) } it "valid" do user = build :cms_user user.should be_valid end end describe "set password" do it "value will convert to md5" do user = build :cms_user user.password = "password" user.password.should eq Digest::MD5.hexdigest("password") end end describe "authenticate with username and password" do let!(:tester) { create :cms_user, username: "user", password: "password" } it "return user with valid user info" do user = CmsUser.authenticate tester.username, "password" user.id.should eq tester.id end it "return nil with invalid user info" do user = CmsUser.authenticate tester.username, "wrong_password" user.should be_nil end endend
Shoulda-matchers-Ruby unit test helper