热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

shoulda-matchers--Ruby单元测试小帮手

详细文档大家可以点击github的官方说明,不过是英文版本哦https:github.comthoughtbotshoulda-matchersshoulda-matcherspr

详细文档大家可以点击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_protectedmacros.
  • 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)
end
end

看下对应的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
end
end

推荐阅读
  • Linux服务器密码过期策略、登录次数限制、私钥登录等配置方法
    本文介绍了在Linux服务器上进行密码过期策略、登录次数限制、私钥登录等配置的方法。通过修改配置文件中的参数,可以设置密码的有效期、最小间隔时间、最小长度,并在密码过期前进行提示。同时还介绍了如何进行公钥登录和修改默认账户用户名的操作。详细步骤和注意事项可参考本文内容。 ... [详细]
  • 向QTextEdit拖放文件的方法及实现步骤
    本文介绍了在使用QTextEdit时如何实现拖放文件的功能,包括相关的方法和实现步骤。通过重写dragEnterEvent和dropEvent函数,并结合QMimeData和QUrl等类,可以轻松实现向QTextEdit拖放文件的功能。详细的代码实现和说明可以参考本文提供的示例代码。 ... [详细]
  • baresip android编译、运行教程1语音通话
    本文介绍了如何在安卓平台上编译和运行baresip android,包括下载相关的sdk和ndk,修改ndk路径和输出目录,以及创建一个c++的安卓工程并将目录考到cpp下。详细步骤可参考给出的链接和文档。 ... [详细]
  • Node.js学习笔记(一)package.json及cnpm
    本文介绍了Node.js中包的概念,以及如何使用包来统一管理具有相互依赖关系的模块。同时还介绍了NPM(Node Package Manager)的基本介绍和使用方法,以及如何通过NPM下载第三方模块。 ... [详细]
  • 大数据Hadoop生态(20)MapReduce框架原理OutputFormat的开发笔记
    本文介绍了大数据Hadoop生态(20)MapReduce框架原理OutputFormat的开发笔记,包括outputFormat接口实现类、自定义outputFormat步骤和案例。案例中将包含nty的日志输出到nty.log文件,其他日志输出到other.log文件。同时提供了一些相关网址供参考。 ... [详细]
  • 阿里Treebased Deep Match(TDM) 学习笔记及技术发展回顾
    本文介绍了阿里Treebased Deep Match(TDM)的学习笔记,同时回顾了工业界技术发展的几代演进。从基于统计的启发式规则方法到基于内积模型的向量检索方法,再到引入复杂深度学习模型的下一代匹配技术。文章详细解释了基于统计的启发式规则方法和基于内积模型的向量检索方法的原理和应用,并介绍了TDM的背景和优势。最后,文章提到了向量距离和基于向量聚类的索引结构对于加速匹配效率的作用。本文对于理解TDM的学习过程和了解匹配技术的发展具有重要意义。 ... [详细]
  • Linux重启网络命令实例及关机和重启示例教程
    本文介绍了Linux系统中重启网络命令的实例,以及使用不同方式关机和重启系统的示例教程。包括使用图形界面和控制台访问系统的方法,以及使用shutdown命令进行系统关机和重启的句法和用法。 ... [详细]
  • 原文地址:https:www.cnblogs.combaoyipSpringBoot_YML.html1.在springboot中,有两种配置文件,一种 ... [详细]
  • 本文介绍了PhysioNet网站提供的生理信号处理工具箱WFDB Toolbox for Matlab的安装和使用方法。通过下载并添加到Matlab路径中或直接在Matlab中输入相关内容,即可完成安装。该工具箱提供了一系列函数,可以方便地处理生理信号数据。详细的安装和使用方法可以参考本文内容。 ... [详细]
  • 如何在服务器主机上实现文件共享的方法和工具
    本文介绍了在服务器主机上实现文件共享的方法和工具,包括Linux主机和Windows主机的文件传输方式,Web运维和FTP/SFTP客户端运维两种方式,以及使用WinSCP工具将文件上传至Linux云服务器的操作方法。此外,还介绍了在迁移过程中需要安装迁移Agent并输入目的端服务器所在华为云的AK/SK,以及主机迁移服务会收集的源端服务器信息。 ... [详细]
  • Go GUIlxn/walk 学习3.菜单栏和工具栏的具体实现
    本文介绍了使用Go语言的GUI库lxn/walk实现菜单栏和工具栏的具体方法,包括消息窗口的产生、文件放置动作响应和提示框的应用。部分代码来自上一篇博客和lxn/walk官方示例。文章提供了学习GUI开发的实际案例和代码示例。 ... [详细]
  • web.py开发web 第八章 Formalchemy 服务端验证方法
    本文介绍了在web.py开发中使用Formalchemy进行服务端表单数据验证的方法。以User表单为例,详细说明了对各字段的验证要求,包括必填、长度限制、唯一性等。同时介绍了如何自定义验证方法来实现验证唯一性和两个密码是否相等的功能。该文提供了相关代码示例。 ... [详细]
  • Vagrant虚拟化工具的安装和使用教程
    本文介绍了Vagrant虚拟化工具的安装和使用教程。首先介绍了安装virtualBox和Vagrant的步骤。然后详细说明了Vagrant的安装和使用方法,包括如何检查安装是否成功。最后介绍了下载虚拟机镜像的步骤,以及Vagrant镜像网站的相关信息。 ... [详细]
  • SpringBoot整合SpringSecurity+JWT实现单点登录
    SpringBoot整合SpringSecurity+JWT实现单点登录,Go语言社区,Golang程序员人脉社 ... [详细]
  • CentOS7.8下编译muduo库找不到Boost库报错的解决方法
    本文介绍了在CentOS7.8下编译muduo库时出现找不到Boost库报错的问题,并提供了解决方法。文章详细介绍了从Github上下载muduo和muduo-tutorial源代码的步骤,并指导如何编译muduo库。最后,作者提供了陈硕老师的Github链接和muduo库的简介。 ... [详细]
author-avatar
无棱9931
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有