老版本的 have_tag、with_tag(以及更多)匹配器用于 rspec 3(由 Nokogiri 提供支持)
老版本的 have_tag、with_tag(以及更多)匹配器用于 rspec 3(由 Nokogiri 提供支持)
have_tag matcher from rspec-rails 1.x, but with own syntactic sugarString (or capybara's page, see below)Add to your Gemfile in the :test group:
gem 'rspec-html-matchers'Include it in your RSpec configuration:
RSpec.configure do |config|
config.include RSpecHtmlMatchers
endor just in your spec(s):
describe "my view spec" do
include RSpecHtmlMatchers
it "has tags" do
expect(rendered).to have_tag('div')
end
endCucumber configuration:
World RSpecHtmlMatchersas this gem requires nokogiri, here are instructions for installing it.
so perhaps your code produces following output:
Simple Form
so you test it with the following:
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
endExample above should be self-descriptive, if not, please refer to the have_tag documentation
Input can be any html string. Let's take a look at these examples:
…special case for classes matching:
# all of this are equivalent:
expect('
Paragraph
').to have_tag('p', :with => { :class => 'qwe rty' })
expect('
Paragraph
').to have_tag('p', :with => { :class => 'rty qwe' })
expect('
Paragraph
').to have_tag('p', :with => { :class => ['rty', 'qwe'] })
expect('
Paragraph
').to have_tag('p', :with => { :class => ['qwe', 'rty'] })The same works with :without:
# all of this are equivalent:
expect('
Paragraph
').to have_tag('p', :without => { :class => 'qwe rty' })
expect('
Paragraph
').to have_tag('p', :without => { :class => 'rty qwe' })
expect('
Paragraph
').to have_tag('p', :without => { :class => ['rty', 'qwe'] })
expect('
Paragraph
').to have_tag('p', :without => { :class => ['qwe', 'rty'] })content matching:
…usage with capybara and cucumber:
expect(page).to have_tag( ... )where page is an instance of Capybara::Session
also included shorthand matchers for form inputs:
and of course you can use the without_ matchers,
for more info take a look at documentation
you can match:
expect(response).to have_tag('div', 'expected content')
expect(response).to have_tag('div', /regexp matching expected content/)RSpec 1 have_tag documentation
You can also match the content of attributes by using selectors. For example, to ensure an img tag has an alt attribute, you can match:
expect(index).to have_tag("img[alt!='']")You can find more on documentation
Also, please read CHANGELOG and issues, might be helpful.
暂无开放 Issues,或尚未同步最近议题。