rspec-html-matchers 技术文档
2024-12-26 00:16:59作者:范靓好Udolf
本文档将详细介绍如何安装、使用以及调用 rspec-html-matchers 项目,帮助用户更好地理解和运用该项目。
1. 安装指南
在项目 Gemfile 中,将以下代码添加到 :test 组中:
gem 'rspec-html-matchers'
接下来,在你的 RSpec 配置文件中包含该库:
RSpec.configure do |config|
config.include RSpecHtmlMatchers
end
或者在特定的 spec 文件中包含它:
describe "my view spec" do
include RSpecHtmlMatchers
it "has tags" do
expect(rendered).to have_tag('div')
end
end
对于 Cucumber 配置,使用以下代码:
World RSpecHtmlMatchers
由于该库依赖于 nokogiri,请参考 nokogiri 安装指南。
2. 项目的使用说明
假设您的代码生成了以下 HTML 输出:
<h1>Simple Form</h1>
<form action="/users" method="post">
<p>
<input type="email" name="user[email]" />
</p>
<p>
<input type="submit" id="special_submit" />
</p>
</form>
您可以这样进行测试:
expect(rendered).to have_tag('form', :with => { :action => '/users', :method => 'post' }) do
with_tag "input", :with => { :name => "user[email]", :type => 'email' }
with_tag "input#special_submit", :count => 1
without_tag "h1", :text => 'unneeded tag'
without_tag "p", :text => /content/i
end
输入可以是任何 HTML 字符串。以下是一些示例:
-
通过 CSS 匹配标签:
expect('<p class="qwe rty" id="qwerty">Paragraph</p>').to have_tag('p') expect('<p class="qwe rty" id="qwerty">Paragraph</p>').to have_tag(:p) expect('<p class="qwe rty" id="qwerty">Paragraph</p>').to have_tag('p#qwerty') expect('<p class="qwe rty" id="qwerty">Paragraph</p>').to have_tag('p.qwe.rty') -
类匹配的特殊情况:
expect('<p class="qwe rty" id="qwerty">Paragraph</p>').to have_tag('p', :with => { :class => 'qwe rty' }) expect('<p class="qwe rty" id="qwerty">Paragraph</p>').to have_tag('p', :with => { :class => 'rty qwe' }) -
内容匹配:
expect('<p> Some content here</p>').to have_tag('p', :text => ' Some content here') expect('<p> Some content here</p>').to have_tag('p', :text => /Some content here/) -
与 capybara 和 cucumber 一起使用:
expect(page).to have_tag( ... )其中
page是 Capybara::Session 的一个实例。 -
还包括表单输入的简写匹配器:
have_formwith_checkboxwith_email_fieldwith_file_fieldwith_hidden_fieldwith_optionwith_password_fieldwith_radio_buttonwith_buttonwith_selectwith_submitwith_text_areawith_text_fieldwith_url_fieldwith_number_fieldwith_range_fieldwith_date_field
当然,您也可以使用
without_匹配器。更多详情请查看 文档。
3. 项目API使用文档
具体 API 使用方法请参考 rspec-html-matchers API 文档。
4. 项目安装方式
项目安装方式已在“安装指南”一节中详细说明。请按照上述步骤进行安装。
登录后查看全文
热门项目推荐
GLM-5智谱 AI 正式发布 GLM-5,旨在应对复杂系统工程和长时域智能体任务。Jinja00
GLM-5-w4a8GLM-5-w4a8基于混合专家架构,专为复杂系统工程与长周期智能体任务设计。支持单/多节点部署,适配Atlas 800T A3,采用w4a8量化技术,结合vLLM推理优化,高效平衡性能与精度,助力智能应用开发Jinja00
jiuwenclawJiuwenClaw 是一款基于openJiuwen开发的智能AI Agent,它能够将大语言模型的强大能力,通过你日常使用的各类通讯应用,直接延伸至你的指尖。Python0208- QQwen3.5-397B-A17BQwen3.5 实现了重大飞跃,整合了多模态学习、架构效率、强化学习规模以及全球可访问性等方面的突破性进展,旨在为开发者和企业赋予前所未有的能力与效率。Jinja00
AtomGit城市坐标计划AtomGit 城市坐标计划开启!让开源有坐标,让城市有星火。致力于与城市合伙人共同构建并长期运营一个健康、活跃的本地开发者生态。01
MarkFlowy一款 AI Markdown 编辑器TSX01
项目优选
收起
deepin linux kernel
C
27
12
OpenHarmony documentation | OpenHarmony开发者文档
Dockerfile
612
4.07 K
Ascend Extension for PyTorch
Python
454
538
本项目是CANN提供的数学类基础计算算子库,实现网络在NPU上加速计算。
C++
924
777
openEuler内核是openEuler操作系统的核心,既是系统性能与稳定性的基石,也是连接处理器、设备与服务的桥梁。
C
374
253
🔥LeetCode solutions in any programming language | 多种编程语言实现 LeetCode、《剑指 Offer(第 2 版)》、《程序员面试金典(第 6 版)》题解
Java
69
21
暂无简介
Dart
858
205
🎉 (RuoYi)官方仓库 基于SpringBoot,Spring Security,JWT,Vue3 & Vite、Element Plus 的前后端分离权限管理系统
Vue
1.48 K
835
React Native鸿蒙化仓库
JavaScript
322
378
AscendNPU-IR是基于MLIR(Multi-Level Intermediate Representation)构建的,面向昇腾亲和算子编译时使用的中间表示,提供昇腾完备表达能力,通过编译优化提升昇腾AI处理器计算效率,支持通过生态框架使能昇腾AI处理器与深度调优
C++
114
177