標籤:des style blog http color io os 使用 ar
詳細文檔大家可以點擊github的官方說明,不過是英文版本哦
https://github.com/thoughtbot/shoulda-matchers
shoulda-matchers provides Test::Unit- and RSpec-compatible one-liners that test common Rails functionality. These tests would otherwise be much longer, more complex, and error-prone.
shoulda-matchers對應了可以測試Rails常規功能的方法,使得測試案例非常簡潔高效
如何安裝使用?
(1)把下面的幾行加到Gemfile檔案裡
group :test do gem ‘shoulda-matchers‘, require: false end其中require:false表示不自動載入
(2)修改spec_helper.rb,加上下面兩行
require ‘rspec/rails‘require ‘shoulda/matchers‘
下面來看一下匹配的內容
ActiveModel Matchers
- allow_mass_assignment_of tests usage of Rails 3‘s
attr_accessible
and attr_protected
macros.
- allow_value tests usage of the
validates_format_of
validation.
- validate_inclusion_of tests usage of
validates_inclusion_of
.
- validate_exclusion_of tests usage of
validates_exclusion_of
.
- ensure_length_of tests usage of
validates_length_of
.
- have_secure_password tests usage of
has_secure_password
.
- validate_confirmation_of tests usage of
validates_confirmation_of
.
- validate_numericality_of tests usage of
validates_numericality_of
.
- validate_presence_of tests usage of
validates_presence_of
.
- validate_uniqueness_of tests usage of
validates_uniqueness_of
.
ActiveRecord Matchers
- accept_nested_attributes_for tests usage of the
accepts_nested_attributes_for
macro.
- belong_to tests your
belongs_to
associations.
- define_enum_for tests usage of the
enum
macro.
- have_many tests your
has_many
associations.
- have_one tests your
has_one
associations.
- have_and_belong_to_many tests your
has_and_belongs_to_many
associations.
- have_db_column tests that the table that backs your model has a specific column.
- have_db_index tests that the table that backs your model has an index on a specific column.
- have_readonly_attribute tests usage of the
attr_readonly
macro.
- serialize tests usage of the
serialize
macro.
ActionController Matchers
- filter_param tests parameter filtering configuration.
- redirect_to tests that an action redirects to a certain location.
- render_template tests that an action renders a template.
- render_with_layout tests that an action is rendered with a certain layout.
- rescue_from tests usage of the
rescue_from
macro.
- respond_with tests that an action responds with a certain status code.
- route tests your routes.
- set_session makes assertions on the
session
hash.
- set_the_flash makes assertions on the
flash
hash.
重頭戲就是下面的例子啦
# -*- 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
看下對應的cms_user_spec.rb測試
# -*- 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單元測試小幫手