On Sun, Aug 23, 2009 at 10:08 AM, nruth<nick.rutherford at gmail.com>
wrote:> I''d appreciate suggestions for improving this matcher. Currently
it
> relies on the link saying "Delete", not whether it sends a delete
> request when used.
>
> ? ?def have_delete_link(href)
> ? ? ?simple_matcher("a link href=''#{href}''") do
|given|
> ? ? ? ?selector =
have_selector("a[href=''#{href}'']", :content =>
> "Delete")
> ? ? ? ?selector.matches? given
> ? ? ?end
> ? ?end
>From the outside, I''d prefer something abstracted to a slightly
higher level:
response.should have_delete_link_for(resource)
and let the matcher figure out what that means.
As for the details, there are a number of facilities in webrat that
can help with this, but they''re all internal to webrat :) What
I''d
like to be able to do (but can''t right now w/ public APIs) is
something like this:
Spec::Matchers.define :have_delete_link_for do |resource|
match do |response|
response.links.any? do |link|
link.href == url_for(resource) &&
link.http_method == :delete
end
end
end
That won''t work because neither rspec nor webrat currently expose the
necessary APIs. You _can_ do this right now, however:
require ''webrat''
Spec::Matchers.define :have_delete_link_for do |resource|
match do |response|
doc = Webrat::XML.document(response.body)
links = Webrat::XML.xpath_search(doc, *Webrat::Link.xpath_search)
links.any? do |link|
webrat_link = Webrat::Link.new(nil, link)
webrat_link.send(:href)
="/#{resource.class.name.underscore.pluralize}/#{resource.id}"
&&
webrat_link.send(:http_method) == :delete
end
end
end
This is admittedly foul in many ways, and makes all sorts of
assumptions - but it actually works and results in a very expressive
matcher. I''ll post a request to the webrat tracker to expose some
methods that can help with this. But feel free to use and improve on
this in the mean time (at your own risk, of course ;) ).
Cheers,
David