I noticed something interesting these days. Even though most Rails websites boasts the benefits of Test Driven Development, most examples they give us don’t test the whole app functionality.
How come? Well, REST was the word of the day, it is now deep inside our brains, right? But have you ever seen an example test of the XML output of Rails? Surely not. Every guide, tutorial and example shows only how to test for the default output, which is HTML.
So what? That ought to be easy, right? Just add :format after the get, post, put or delete function and you will be fine, you must think. Well, I have been working on a project and had to test if the Atom Feed would work ok. I must say it was one of the most frustrating tests I have ever written.
Turns out that, even though you can do something like this:
formatted_posts_url(:format => :atom)
you CAN’T do that on testes:
get :index, :format => :atom
You know why? Because, even though you CAN use symbols (:atom) on your code, you CAN’T use on tests FOR THE FORMAT. Let’s rephrase it: You can use symbols on tests when specifying the action, the parameters and everything else BUT NOT THE FORMAT of the request. So the code above will give you a mysterious HTTP 406 Error Code.
So, to prevent you from loosing a full day where you felt productive, like I did, the following code will work:
get :index, :format => "atom"