Ruby 如何编写级联RSpec匹配器
现在我有以下代码:Ruby 如何编写级联RSpec匹配器,ruby,rspec,rspec2,Ruby,Rspec,Rspec2,现在我有以下代码: 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, attr
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'})
有没有一种方法可以在自定义匹配器中实现连续性?请参阅
html.should include_an_html_tag(:a).with_html_attributes('data-remote' => 'true', 'data-method' => 'post')