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

rspec-html-matchers 技术文档

2024-12-26 03:14:44作者:范靓好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. 项目安装方式

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

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

项目优选

收起
kernelkernel
deepin linux kernel
C
22
6
docsdocs
OpenHarmony documentation | OpenHarmony开发者文档
Dockerfile
165
2.05 K
nop-entropynop-entropy
Nop Platform 2.0是基于可逆计算理论实现的采用面向语言编程范式的新一代低代码开发平台,包含基于全新原理从零开始研发的GraphQL引擎、ORM引擎、工作流引擎、报表引擎、规则引擎、批处理引引擎等完整设计。nop-entropy是它的后端部分,采用java语言实现,可选择集成Spring框架或者Quarkus框架。中小企业可以免费商用
Java
8
0
openHiTLS-examplesopenHiTLS-examples
本仓将为广大高校开发者提供开源实践和创新开发平台,收集和展示openHiTLS示例代码及创新应用,欢迎大家投稿,让全世界看到您的精巧密码实现设计,也让更多人通过您的优秀成果,理解、喜爱上密码技术。
C
85
562
leetcodeleetcode
🔥LeetCode solutions in any programming language | 多种编程语言实现 LeetCode、《剑指 Offer(第 2 版)》、《程序员面试金典(第 6 版)》题解
Java
60
17
apintoapinto
基于golang开发的网关。具有各种插件,可以自行扩展,即插即用。此外,它可以快速帮助企业管理API服务,提高API服务的稳定性和安全性。
Go
22
0
cjoycjoy
一个高性能、可扩展、轻量、省心的仓颉应用开发框架。IoC,Rest,宏路由,Json,中间件,参数绑定与校验,文件上传下载,OAuth2,MCP......
Cangjie
94
15
ohos_react_nativeohos_react_native
React Native鸿蒙化仓库
C++
199
279
giteagitea
喝着茶写代码!最易用的自托管一站式代码托管平台,包含Git托管,代码审查,团队协作,软件包和CI/CD。
Go
17
0
RuoYi-Vue3RuoYi-Vue3
🎉 (RuoYi)官方仓库 基于SpringBoot,Spring Security,JWT,Vue3 & Vite、Element Plus 的前后端分离权限管理系统
Vue
954
564