Just A Summary : Tag testing, everything about testing http://bofh.org.uk/articles/tag/testing.rss en-us 40 Piers Cawley Practices Punditry Testing Your Assumptions <p>One of the joys of writing applications using Ruby on Rails is the way the framework is constantly evolving better ways of doing stuff. It&#8217;s one of the dangers too.</p> <p>Each release of Rails brings new and groovy features to the table, so it&#8217;s nice to stay close to the edge. However, when you do that, a change in the framework can bring your whole app crashing down because a key assumption you made turns out not to be true any more.</p> <p>This is especially common when the assumed behaviour is undocumented, or a surprising consequence of behaviour that <em>is</em> documented.</p> <p>When this happens, your test suite can have failures everywhere; it&#8217;s hard to work out precisely what the underlying problem is.</p> <p>So, what to do?</p> <p>Easy. Write tests that express your assumptions. It&#8217;s always good to make assumptions explicit. It&#8217;s better still to make them executable. Whenever you use something that&#8217;s a little bit surprising or hard to find in the docs, or when you do something that works around a bug, write a test.</p> <p>Then, when the framework changes and tests are failing all over the place, test your assumptions. Hopefully there&#8217;ll be one or two failing tests there that point you to a solution to your problem. If there aren&#8217;t, then at least you know what&#8217;s <em>not</em> causing the problem. And, when you do find the underlying problem, you can add the new assumption to your tests for later.</p> <p>I&#8217;ve just been working on Typo and found (rather usefully as it happens) that, if you add counter caching to a model, the model that holds the cache field will call all its save/update hooks whenever the cache field is updated.</p> <p>Which is a little odd; it could be argued that simply adding what should be an &#8216;invisible&#8217; cache field to a model shouldn&#8217;t go changing its behaviour in this way. So, I&#8217;ve created an <code>assumptions_test.rb</code> file and written the behaviour up as a test. Now, if <a href="http://www.loudthinking.com/">David Heinemeier Hansson</a> decides that this is undesirable behaviour and changes it, we&#8217;ll be able to see what&#8217;s going on.</p> <p>The idea (as is so often the case) comes from Kent Beck. In this case, from <cite asin="0321146530">Test Driven Development</cite>. One more book by Beck that deserves to be on every programmer&#8217;s bookshelf.</p> Sun, 04 Dec 2005 09:37:00 -0600 urn:uuid:9d09cbd7-82de-4c78-92b6-5aa215038cba pdcawley@bofh.org.uk (Piers Cawley) http://www.bofh.org.uk/articles/2005/12/04/testing-your-assumptions#comments The Practice of Programming Ruby Typo agilemethods ruby testing typo rubyonrails http://www.bofh.org.uk/trackbacks?article_id=testing-your-assumptions&day=04&month=12&year=2005 http://www.bofh.org.uk/articles/2005/12/04/testing-your-assumptions