首页
/ rspec-html-matchers 技术文档

rspec-html-matchers 技术文档

2024-12-26 11:31:32作者:范靓好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&nbsphere</p>').to have_tag('p', :text => ' Some content here')
    expect('<p> Some content&nbsphere</p>').to have_tag('p', :text => /Some content here/)
    
  • 与 capybara 和 cucumber 一起使用:

    expect(page).to have_tag( ... )
    

    其中 page 是 Capybara::Session 的一个实例。

  • 还包括表单输入的简写匹配器:

    • have_form
    • with_checkbox
    • with_email_field
    • with_file_field
    • with_hidden_field
    • with_option
    • with_password_field
    • with_radio_button
    • with_button
    • with_select
    • with_submit
    • with_text_area
    • with_text_field
    • with_url_field
    • with_number_field
    • with_range_field
    • with_date_field

    当然,您也可以使用 without_ 匹配器。更多详情请查看 文档

3. 项目API使用文档

具体 API 使用方法请参考 rspec-html-matchers API 文档

4. 项目安装方式

项目安装方式已在“安装指南”一节中详细说明。请按照上述步骤进行安装。

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

项目优选

收起
Cangjie-ExamplesCangjie-Examples
本仓将收集和展示高质量的仓颉示例代码,欢迎大家投稿,让全世界看到您的妙趣设计,也让更多人通过您的编码理解和喜爱仓颉语言。
Cangjie
338
1.19 K
RuoYi-Vue3RuoYi-Vue3
🎉 (RuoYi)官方仓库 基于SpringBoot,Spring Security,JWT,Vue3 & Vite、Element Plus 的前后端分离权限管理系统
Vue
898
534
ohos_react_nativeohos_react_native
React Native鸿蒙化仓库
C++
188
265
kernelkernel
deepin linux kernel
C
22
6
openGauss-serveropenGauss-server
openGauss kernel ~ openGauss is an open source relational database management system
C++
140
188
openHiTLSopenHiTLS
旨在打造算法先进、性能卓越、高效敏捷、安全可靠的密码套件,通过轻量级、可剪裁的软件技术架构满足各行业不同场景的多样化要求,让密码技术应用更简单,同时探索后量子等先进算法创新实践,构建密码前沿技术底座!
C
374
387
CangjieCommunityCangjieCommunity
为仓颉编程语言开发者打造活跃、开放、高质量的社区环境
Markdown
1.09 K
0
note-gennote-gen
一款跨平台的 Markdown AI 笔记软件,致力于使用 AI 建立记录和写作的桥梁。
TSX
86
4
nop-entropynop-entropy
Nop Platform 2.0是基于可逆计算理论实现的采用面向语言编程范式的新一代低代码开发平台,包含基于全新原理从零开始研发的GraphQL引擎、ORM引擎、工作流引擎、报表引擎、规则引擎、批处理引引擎等完整设计。nop-entropy是它的后端部分,采用java语言实现,可选择集成Spring框架或者Quarkus框架。中小企业可以免费商用
Java
7
0
arkanalyzerarkanalyzer
方舟分析器:面向ArkTS语言的静态程序分析框架
TypeScript
114
45