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

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&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. 项目安装方式

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

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

项目优选

收起
atomcodeatomcode
Claude 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 Started
Rust
456
83
docsdocs
暂无描述
Dockerfile
691
4.48 K
kernelkernel
openEuler内核是openEuler操作系统的核心,既是系统性能与稳定性的基石,也是连接处理器、设备与服务的桥梁。
C
409
329
pytorchpytorch
Ascend Extension for PyTorch
Python
552
675
kernelkernel
deepin linux kernel
C
28
16
RuoYi-Vue3RuoYi-Vue3
🎉 (RuoYi)官方仓库 基于SpringBoot,Spring Security,JWT,Vue3 & Vite、Element Plus 的前后端分离权限管理系统
Vue
1.59 K
930
ops-mathops-math
本项目是CANN提供的数学类基础计算算子库,实现网络在NPU上加速计算。
C++
955
931
communitycommunity
本项目是CANN开源社区的核心管理仓库,包含社区的治理章程、治理组织、通用操作指引及流程规范等基础信息
653
232
openHiTLSopenHiTLS
旨在打造算法先进、性能卓越、高效敏捷、安全可靠的密码套件,通过轻量级、可剪裁的软件技术架构满足各行业不同场景的多样化要求,让密码技术应用更简单,同时探索后量子等先进算法创新实践,构建密码前沿技术底座!
C
1.08 K
564
Cangjie-ExamplesCangjie-Examples
本仓将收集和展示高质量的仓颉示例代码,欢迎大家投稿,让全世界看到您的妙趣设计,也让更多人通过您的编码理解和喜爱仓颉语言。
C
436
4.44 K