11 January 2007

Integration Testing Rails

After reading Jamis’s excellent blog about integration testing a ruby on rails app, I decided to take the plunge and write my first integration test. I have never cared much for testing, but with the wonderful testing architecture set up in rails, I have started to actually enjoy it. I now write many tests before I finish coding anything else. I have found it to be very useful and I suspect it has saved me a lot of debugging time. I was kind of forced into this recently because there was no way to test my most recent code with a browser for our iMemoryBook product. The only way I could actually see if what I was writing worked at all was to write a series of functional tests. Today, though, I found I needed to test how two controllers worked together. Hence my need for learning about integration tests.

Duane had written one integration test already so I had a good example to go by, but there was one thing that I did not know how to do. This is why I am writing now, to share what I learned.

05 January 2007

Setting the Accept header

Our site is running on edge rails and we are using the new REST interface. The site also uses a lot of Ajax to make requests that only accept XML as a response. Setting the accept header turned out to be very easy…Just add the header information to the third parameter of the request, which is a hash of HTTP headers.

put "/bookshelf/1/sections/5",
{ :lp => 0,
:pvW => 300,
"section[title]" => "New title",
"section[body]" => "new very short body" },
{ "Accept" => "text/xml" }

In this example I invoke the sections_controller’s update method because of the put call and I will receive my response in XML because I set the Accept header to “text/xml”. Now that I understand this, I can write integration tests for the rest of the application.