Shoulda Matchers 技术文档
2024-12-20 19:09:20作者:冯梦姬Eddie
安装指南
1. 添加 Gem 到 Gemfile
首先,将 shoulda-matchers 添加到你的 Gemfile 中。根据你使用的测试框架(RSpec 或 Minitest),添加相应的配置。
使用 RSpec
group :test do
gem 'shoulda-matchers', '~> 6.0'
end
使用 Minitest
group :test do
gem 'shoulda-matchers', '~> 6.0'
end
2. 安装 Gem
运行以下命令来安装 Gem:
bundle install
3. 配置 Gem
根据你使用的测试框架和项目类型(Rails 或非 Rails),进行相应的配置。
Rails 项目(RSpec)
在 spec/rails_helper.rb 文件底部添加以下配置:
Shoulda::Matchers.configure do |config|
config.integrate do |with|
with.test_framework :rspec
with.library :rails
end
end
非 Rails 项目(RSpec)
在 spec/spec_helper.rb 文件底部添加以下配置:
Shoulda::Matchers.configure do |config|
config.integrate do |with|
with.test_framework :rspec
with.library :active_record
with.library :active_model
end
end
Rails 项目(Minitest)
在 test/test_helper.rb 文件底部添加以下配置:
Shoulda::Matchers.configure do |config|
config.integrate do |with|
with.test_framework :minitest
with.library :rails
end
end
非 Rails 项目(Minitest)
在 test/test_helper.rb 文件底部添加以下配置:
Shoulda::Matchers.configure do |config|
config.integrate do |with|
with.test_framework :minitest
with.library :active_record
with.library :active_model
end
end
项目的使用说明
1. 基本使用
Shoulda Matchers 提供了许多用于测试 Rails 应用的简便方法。你可以在模型、控制器等不同部分使用这些匹配器。
示例:模型测试
# RSpec
RSpec.describe MenuItem, type: :model do
describe 'associations' do
it { should belong_to(:category).class_name('MenuCategory') }
end
describe 'validations' do
it { should validate_presence_of(:name) }
it { should validate_uniqueness_of(:name).scoped_to(:category_id) }
end
end
# Minitest
class MenuItemTest < ActiveSupport::TestCase
context 'associations' do
should belong_to(:category).class_name('MenuCategory')
end
context 'validations' do
should validate_presence_of(:name)
should validate_uniqueness_of(:name).scoped_to(:category_id)
end
end
2. 自定义 subject
在某些情况下,你可能需要自定义 subject。例如,在测试模型验证时,通常会提供一个有效的模型实例,而不是一个全新的实例。
# RSpec
RSpec.describe Post, type: :model do
describe 'validations' do
subject { build(:post) }
it { should validate_presence_of(:title) }
end
end
# Minitest
class PostTest < ActiveSupport::TestCase
context 'validations' do
subject { build(:post) }
should validate_presence_of(:title)
end
end
项目 API 使用文档
1. ActiveModel 匹配器
用于测试 ActiveModel 的匹配器,例如:
validate_presence_ofvalidate_uniqueness_of
2. ActiveRecord 匹配器
用于测试 ActiveRecord 的匹配器,例如:
belong_tohave_many
3. ActionController 匹配器
用于测试控制器的匹配器,例如:
render_templatehave_http_status
4. 独立匹配器
用于测试 Rails 特定功能的匹配器,例如:
delegate
项目安装方式
1. 通过 Gemfile 安装
将 shoulda-matchers 添加到 Gemfile 中,然后运行 bundle install。
2. 手动安装
你也可以通过以下命令手动安装 Gem:
gem install shoulda-matchers
3. 配置
根据你使用的测试框架和项目类型,进行相应的配置。配置文件通常位于 spec/rails_helper.rb 或 test/test_helper.rb 中。
通过以上步骤,你可以成功安装并配置 Shoulda Matchers,开始使用它来简化你的 Rails 测试。
登录后查看全文
热门项目推荐
相关项目推荐
GLM-5智谱 AI 正式发布 GLM-5,旨在应对复杂系统工程和长时域智能体任务。Jinja00
GLM-5-w4a8GLM-5-w4a8基于混合专家架构,专为复杂系统工程与长周期智能体任务设计。支持单/多节点部署,适配Atlas 800T A3,采用w4a8量化技术,结合vLLM推理优化,高效平衡性能与精度,助力智能应用开发Jinja00
jiuwenclawJiuwenClaw 是一款基于openJiuwen开发的智能AI Agent,它能够将大语言模型的强大能力,通过你日常使用的各类通讯应用,直接延伸至你的指尖。Python0194- QQwen3.5-397B-A17BQwen3.5 实现了重大飞跃,整合了多模态学习、架构效率、强化学习规模以及全球可访问性等方面的突破性进展,旨在为开发者和企业赋予前所未有的能力与效率。Jinja00
AtomGit城市坐标计划AtomGit 城市坐标计划开启!让开源有坐标,让城市有星火。致力于与城市合伙人共同构建并长期运营一个健康、活跃的本地开发者生态。01
awesome-zig一个关于 Zig 优秀库及资源的协作列表。Makefile00
项目优选
收起
deepin linux kernel
C
27
12
OpenHarmony documentation | OpenHarmony开发者文档
Dockerfile
602
4.04 K
🔥LeetCode solutions in any programming language | 多种编程语言实现 LeetCode、《剑指 Offer(第 2 版)》、《程序员面试金典(第 6 版)》题解
Java
69
21
暂无简介
Dart
847
204
🎉 (RuoYi)官方仓库 基于SpringBoot,Spring Security,JWT,Vue3 & Vite、Element Plus 的前后端分离权限管理系统
Vue
1.46 K
826
Nop Platform 2.0是基于可逆计算理论实现的采用面向语言编程范式的新一代低代码开发平台,包含基于全新原理从零开始研发的GraphQL引擎、ORM引擎、工作流引擎、报表引擎、规则引擎、批处理引引擎等完整设计。nop-entropy是它的后端部分,采用java语言实现,可选择集成Spring框架或者Quarkus框架。中小企业可以免费商用
Java
12
1
喝着茶写代码!最易用的自托管一站式代码托管平台,包含Git托管,代码审查,团队协作,软件包和CI/CD。
Go
24
0
本项目是CANN提供的数学类基础计算算子库,实现网络在NPU上加速计算。
C++
922
770
🎉 基于Spring Boot、Spring Cloud & Alibaba、Vue3 & Vite、Element Plus的分布式前后端分离微服务架构权限管理系统
Vue
234
152
昇腾LLM分布式训练框架
Python
130
156