rspec-html

0.3.5
Light Mode Dark Mode

Counters

The match_text and contain_tag matchers support a counting interface to verify the number of occurrences of a given element specification within a document.

The following methods can be added to a match_text or contain_tag call:

  • #once
  • #twice
  • #times(n)
  • #at_least(:once), #at_least(:twice), #at_least.times(n)
  • #at_most(:once), #at_most(:twice), #at_most.times(n)

Exact Counts

subject(:document) { parse_html('<div>my div</div>') }

it 'has one div' do
  expect(document.div).to match_text('my div').once
end
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html><body>
    <div>my div</div>
  </body></html>
my div
subject(:document) { parse_html('<div>my div</div><div>my div</div>') }

it 'has one div' do
  expect(document.div).to match_text('my div').twice
end
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html><body>
    <div>my div</div>
    <div>my div</div>
  </body></html>
my div
my div
subject(:document) { parse_html('<div>my div</div><div>my div</div><div>my div</div>') }

it 'has one div' do
  expect(document.div).to match_text('my div').times(3)
end
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html><body>
    <div>my div</div>
    <div>my div</div>
    <div>my div</div>
  </body></html>
my div
my div
my div

Minimum/Maximum Counts

subject(:document) { parse_html('<div>my div</div><div>my div</div><div>my div</div>') }

it 'has at least two divs' do
  expect(document.div).to match_text('my div').at_least(:twice)
end
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html><body>
    <div>my div</div>
    <div>my div</div>
    <div>my div</div>
  </body></html>
my div
my div
my div
subject(:document) { parse_html('<div>my div</div><div>my div</div><div>my div</div>') }

it 'has at most three divs' do
  expect(document.div).to match_text('my div').at_most.times(3)
end
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html><body>
    <div>my div</div>
    <div>my div</div>
    <div>my div</div>
  </body></html>
my div
my div
my div

Documentation generated by rspec-documentation