首页
/ Shoulda Matchers 技术文档

Shoulda Matchers 技术文档

2024-12-20 03:17:41作者:冯梦姬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_of
  • validate_uniqueness_of

2. ActiveRecord 匹配器

用于测试 ActiveRecord 的匹配器,例如:

  • belong_to
  • have_many

3. ActionController 匹配器

用于测试控制器的匹配器,例如:

  • render_template
  • have_http_status

4. 独立匹配器

用于测试 Rails 特定功能的匹配器,例如:

  • delegate

项目安装方式

1. 通过 Gemfile 安装

shoulda-matchers 添加到 Gemfile 中,然后运行 bundle install

2. 手动安装

你也可以通过以下命令手动安装 Gem:

gem install shoulda-matchers

3. 配置

根据你使用的测试框架和项目类型,进行相应的配置。配置文件通常位于 spec/rails_helper.rbtest/test_helper.rb 中。

通过以上步骤,你可以成功安装并配置 Shoulda Matchers,开始使用它来简化你的 Rails 测试。

登录后查看全文
热门项目推荐

热门内容推荐

最新内容推荐

项目优选

收起
ohos_react_nativeohos_react_native
React Native鸿蒙化仓库
C++
178
262
RuoYi-Vue3RuoYi-Vue3
🎉 (RuoYi)官方仓库 基于SpringBoot,Spring Security,JWT,Vue3 & Vite、Element Plus 的前后端分离权限管理系统
Vue
868
513
openGauss-serveropenGauss-server
openGauss kernel ~ openGauss is an open source relational database management system
C++
129
183
openHiTLSopenHiTLS
旨在打造算法先进、性能卓越、高效敏捷、安全可靠的密码套件,通过轻量级、可剪裁的软件技术架构满足各行业不同场景的多样化要求,让密码技术应用更简单,同时探索后量子等先进算法创新实践,构建密码前沿技术底座!
C
268
308
HarmonyOS-ExamplesHarmonyOS-Examples
本仓将收集和展示仓颉鸿蒙应用示例代码,欢迎大家投稿,在仓颉鸿蒙社区展现你的妙趣设计!
Cangjie
398
373
CangjieCommunityCangjieCommunity
为仓颉编程语言开发者打造活跃、开放、高质量的社区环境
Markdown
1.07 K
0
ShopXO开源商城ShopXO开源商城
🔥🔥🔥ShopXO企业级免费开源商城系统,可视化DIY拖拽装修、包含PC、H5、多端小程序(微信+支付宝+百度+头条&抖音+QQ+快手)、APP、多仓库、多商户、多门店、IM客服、进销存,遵循MIT开源协议发布、基于ThinkPHP8框架研发
JavaScript
93
15
note-gennote-gen
一款跨平台的 Markdown AI 笔记软件,致力于使用 AI 建立记录和写作的桥梁。
TSX
83
4
cherry-studiocherry-studio
🍒 Cherry Studio 是一款支持多个 LLM 提供商的桌面客户端
TypeScript
599
58
GitNextGitNext
基于可以运行在OpenHarmony的git,提供git客户端操作能力
ArkTS
10
3