[rspec-users] how to catch a missing image (rspec-rails)

Evan Dorn lists at ruby-forum.com
Thu Oct 2 23:43:49 EDT 2008

I want to make sure all external resources called for by my views (like
images) actually exist.  How do I write an rspec that will fail when an
image included on the page isn't present?

For example, let's say in application.html.erb, I include <img
src="/images/mylogo.png">, but that file doesn't exist (say it's
actually /images/mylogo.jpg).

When this is run on the server, the image won't appear, and the server
log will show an error like this:

 >> ActionController::RoutingError (No route matches
"/images/mylogo.png" with
 >> {:method=>:get}):

However, views tests like response.should have_tag(img[src=/mylogo.png/)
won't catch it, because the tag will be there.    controllers tests
(with integrate_views on) for response.should be_success,
render_template won't catch it because the page itself was a success and
rendered the template.

But an error was thrown during a second connection to the server,
because the image file didn't exist ... there must be a way for any such
failures.  How?

Posted via http://www.ruby-forum.com/.

More information about the rspec-users mailing list