1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
|
# frozen_string_literal: true
RSpec::Matchers.define :have_attribute do |attribute|
@selector = "body > *:first"
chain :at_selector do |selector|
@selector = selector
end
match do |document|
name, expected = attribute.first
expected == attribute(document, name)
end
description { "have attribute #{attribute.inspect} at selector #{@selector.inspect}" }
failure_message do |document|
name, expected = attribute.first
"expected #{name} attribute at #{@selector.inspect} to be #{expected.inspect} but was #{attribute(document, name).inspect}"
end
failure_message_when_negated do |document|
name, expected = attribute.first
"expected #{name} attribute at #{@selector.inspect} to not be #{expected.inspect}"
end
def attribute(document, attribute_name)
node = document.css(@selector).first
node && node[attribute_name]
end
end
|