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. 项目安装方式
项目安装方式已在“安装指南”一节中详细说明。请按照上述步骤进行安装。
登录后查看全文
热门项目推荐
atomcodeClaude Code 的开源替代方案。连接任意大模型,编辑代码,运行命令,自动验证 — 全自动执行。用 Rust 构建,极致性能。 | An open-source alternative to Claude Code. Connect any LLM, edit code, run commands, and verify changes — autonomously. Built in Rust for speed. Get StartedRust0231
GLM-5.2智谱开源 GLM-5.2,这是针对长文本任务的最新旗舰模型。相较于前代产品 GLM-5.1,它在长文本任务处理能力上实现了显著飞跃,并且首次在稳定的 100 万 token 上下文中提供这一能力。Jinja00
JoyAI-VL-Interaction-Preview京东开源首个开源、视觉驱动的实时交互模型——它能实时监控视频流,并自主决定何时发言、保持沉默或委托任务。Jinja00
cann-learning-hubCANN 学习中心仓,支持在线互动运行、边学边练,提供教程、示例与优化方案,一站式助力昇腾开发者快速上手。Jupyter Notebook0151
kornia🐍 空间人工智能的几何计算机视觉库Python02
PaddleParallel Distributed Deep Learning: Machine Learning Framework from Industrial Practice (『飞桨』核心框架,深度学习&机器学习高性能单机、分布式训练和跨平台部署)C++02
热门内容推荐
最新内容推荐
项目优选
收起
暂无描述
Dockerfile
782
5.11 K
本项目是CANN提供的transformer类大模型算子库,实现网络在NPU上加速计算。
C++
892
2.06 K
openEuler内核是openEuler操作系统的核心,既是系统性能与稳定性的基石,也是连接处理器、设备与服务的桥梁。
C
471
473
Ascend Extension for PyTorch
Python
764
972
本项目是CANN提供的神经网络类计算算子库,实现网络在NPU上加速计算。
C++
710
1.43 K
deepin linux kernel
C
32
16
CANN 学习中心仓,支持在线互动运行、边学边练,提供教程、示例与优化方案,一站式助力昇腾开发者快速上手。
Jupyter Notebook
432
151
本项目是CANN提供的数学类基础计算算子库,实现网络在NPU上加速计算。
C++
1.11 K
1.15 K
JiuwenSwarm 是一款基于openJiuwen开发的智能AI Agent,它能够将大语言模型的强大能力,通过你日常使用的各类通讯应用,直接延伸至你的指尖。
Python
2.27 K
681
本仓库是 Flutter SDK 与 Flutter Engine 的 OpenHarmony 适配版本,由 CPF-Flutter 团队维护。开发者可使用熟悉的 Flutter 技术栈开发 OpenHarmony 应用,3.35.7 及以后的适配版本可基于本仓库源码构建支持 OpenHarmony 的 Flutter Engine。
Dart
1.04 K
272