Right now I have the following piece of code:
RSpec::Matchers.define :include_an_html_tag do |tag|
tag = Regexp.escape(tag.to_s)
match do |html|
html.to_s =~ %r[<#{tag}( [^>]+)?>.*?</#{tag}>]m
end
end
RSpec::Matchers.define :have_html_attributes do |tag, attributes_hash|
match do |html|
attributes_hash.all? do |k,v|
html.to_s =~ %r[<#{Regexp.escape tag.to_s}( [^>]+)? #{Regexp.escape k.to_s}\="#{Regexp.escape v.to_s}"( [^>]+)?>]m
end
end
end
html.should include_an_html_tag(:a)
html.should include_an_html_tag(:script)
html.should have_html_attributes(:a, {'data-remote' => 'true', 'data-method' => 'post'})
and it works pretty fine.
What I want to do is have my matchers cascaded, like so:
html.should include_an_html_tag(:a).with_html_attributes('data-remote' => 'true', 'data-method' => 'post')
Is there a way to achieve succession in custom matchers?
See http://relishapp.com/rspec/rspec-expectations/dir/custom-matchers/define-matcher-with-fluent-interface.