Ryan Bigg

⟵ Posts

Webrat Woes

30 Jan 2009

Yesterday I wrote a feature for one of the apps at work that was fairly simple it contained this line:

  clicks_link("I am done")

The page contained a link with the text exactly as “I am done” and in all dimensions of this universe you would expect this to pass without fault. It didn’t.

So when this feature failed I brought Bo over and got him to look at it. We couldn’t figure it out yesterday and it took a few hours this morning to figure out what it was. The site was fairly basic, consisting of a layout that contained:

<%= link_to "Things", thing_path %>
<%= yield %>

And the page itself consisted of a form and ended in the link:

<%= link_to "I am done", do_something_path %>

But the bloody feature failed! So we sanity check’d it, output the response’s body and definitely saw the a tag in there. It was there! So we further checked it and tried the URL from the layout and that worked! Major WTF moment there. It took us an hour or so of further WTF before Bo said “Humour me” and gave the layout valid HTML, you know, something like this:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">

<html lang="en">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
  <%= link_to "Things", things_path %>
  <%= yield %>
</body>
</html>

And the feature passed! So remember kids, do not be lazy! If you’re testing with webrat remember to always have a valid HTML layout on your site!