Shoulda-matchers-Ruby unit test helper

Source: Internet
Author: User

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'sattr_accessibleAndattr_protectedMacros.
  • Allow_valueTests usage ofvalidates_format_ofValidation.
  • Validate_inclusion_ofTests usagevalidates_inclusion_of.
  • Validate_exclusion_ofTests usagevalidates_exclusion_of.
  • Ensure_length_ofTests usagevalidates_length_of.
  • Have_secure_passwordTests usagehas_secure_password.
  • Validate_confirmation_ofTests usagevalidates_confirmation_of.
  • Validate_numericality_ofTests usagevalidates_numericality_of.
  • Validate_presence_ofTests usagevalidates_presence_of.
  • Validate_uniqueness_ofTests usagevalidates_uniqueness_of.
Activerecord matchers
  • Accept_nested_attributes_forTests usage ofaccepts_nested_attributes_forMacro.
  • Belong_toTests yourbelongs_toAssociations.
  • Define_enum_forTests usage ofenumMacro.
  • Have_tablesTests yourhas_manyAssociations.
  • Have_oneTests yourhas_oneAssociations.
  • Have_and_belong_to_propTests yourhas_and_belongs_to_manyAssociations.
  • 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 ofattr_readonlyMacro.
  • SerializeTests usage ofserializeMacro.
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 ofrescue_fromMacro.
  • Respond_withTests that an action responds with a certain status code.
  • RouteTests your routes.
  • Set_sessionMakes assertions onsessionHash.
  • Set_the_flashMakes assertions onflashHash.

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.