{"id":168,"date":"2010-04-16T10:14:30","date_gmt":"2010-04-16T10:14:30","guid":{"rendered":"http:\/\/nicholshayes.myzen.co.uk\/blog\/?p=168"},"modified":"2010-04-16T10:18:24","modified_gmt":"2010-04-16T10:18:24","slug":"test-driven-development-is-essential","status":"publish","type":"post","link":"http:\/\/nicholshayes.co.uk\/blog\/?p=168","title":{"rendered":"Test driven development is essential"},"content":{"rendered":"<p>After almost a year of using test driven development, I am now a total convert. I now feel it is so vital to good development practice that, across the board, I think I can label my previous applications as bad just because they don&#8217;t have rigorously tested tests. I was a sinner, but I have seen the light.<\/p>\n<p>I&#8217;ve just had a good example of why test driven development is so important.<\/p>\n<p>I have created a dynamic test method system that allows me write functional tests a little quicker. For example, one of the methods is &#8220;assert_item_passed_to_template&#8221;. I use method_missing to catch methods that match the method name pattern but replace &#8216;item&#8217; with the name of the item they want tested. So assert_user_passed_to_template looks for a variable @user and matches it to the contents of assigns(:user), returning true if they match, and an error that describes the mismatch if they don&#8217;t.<\/p>\n<p>One of the methods I use a lot was called assert_populated_item_passed_to_template. This didn&#8217;t match @user against assigns(:user) but instead matched assigns(:user) to @user_params (which is in effect params[:user]). So useful to make sure that the object returned to a failed save still contains the entered data.<\/p>\n<p>However, I&#8217;ve been working on another part of the application for a week or two and have now returned to add some more edit functions. And I found the key world &#8216;populated&#8217; confused me. I tried to use assert_populated_user_passed_to_template where I should have been using assert_user_passed_to_template. It took me a few minutes to work out why my my tests were failing. Populated is the wrong word. A @user object generated from the database is &#8216;populated&#8217;. I realised that if I was confused by populated (and I created the method name) so would anyone else reading my code.<\/p>\n<p>However, every new and edit method in my application was tested with an assert_populated_item_passed_to_template call. What&#8217;s worse, because of the dynamic use of this method, I could not just rename the method; I had to rename all the methods with that pattern.<\/p>\n<p>In the past that would have been a no, no. Just too much work to make sure the change hadn&#8217;t broken other parts of the application.<\/p>\n<p>However, with this application I have tested from the start, so it was a straight forward task. I simply did a cross project replace of &#8216;assert_populated_&#8217; with &#8216;assert_params_loaded_&#8217; so that the method became &#8216;assert_params_loaded_item_passed_to_template&#8217;. Then ran all my tests. As they all passed, I could be sure the change hadn&#8217;t had any adverse knock on effects.<\/p>\n<p>This means when I identify a way of improving my application, I feel much safer implementing that change than I did before I unit tested. So my application gets better as I work on it. <\/p>\n<p>If you don&#8217;t test as you code, you don&#8217;t get a reliable set of tests that you know work, and you don&#8217;t get this vital safety net. Test driven development is up there with having source control, as vital prerequisites to good coding practice in my opinion. You don&#8217;t have to do it, but if you don&#8217;t you&#8217;ll regret it later.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>After almost a year of using test driven development, I am now a total convert. I now feel it is so vital to good development practice that, across the board, I think I can label my previous applications as bad &hellip; <a href=\"http:\/\/nicholshayes.co.uk\/blog\/?p=168\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[3],"tags":[],"_links":{"self":[{"href":"http:\/\/nicholshayes.co.uk\/blog\/index.php?rest_route=\/wp\/v2\/posts\/168"}],"collection":[{"href":"http:\/\/nicholshayes.co.uk\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/nicholshayes.co.uk\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/nicholshayes.co.uk\/blog\/index.php?rest_route=\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"http:\/\/nicholshayes.co.uk\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=168"}],"version-history":[{"count":3,"href":"http:\/\/nicholshayes.co.uk\/blog\/index.php?rest_route=\/wp\/v2\/posts\/168\/revisions"}],"predecessor-version":[{"id":171,"href":"http:\/\/nicholshayes.co.uk\/blog\/index.php?rest_route=\/wp\/v2\/posts\/168\/revisions\/171"}],"wp:attachment":[{"href":"http:\/\/nicholshayes.co.uk\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=168"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/nicholshayes.co.uk\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=168"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/nicholshayes.co.uk\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=168"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}