home

Boost Blog

Writing tests/specs for existing code

Posted by jeremy on June 22nd, 2009

On several occasions we’ve looked at Rails projects with no tests, or very low test coverage. Trying to write tests when you didn’t write the original methods is difficult. Here are some tips that we’ve found useful.

Use your favourite framework.

If there is a large body of existing tests using a particular framework, write your new tests in that framework. If there are few or no tests then strip out anything already there and replace it with your preferred framework. At Boost we’ve decided to use RSpec for all our projects. Your objective here is to write tests, not to learn a new way of writing tests.

Don’t fix bugs!

If you discover what you believe to be a bug while writing tests, consider that you don’t know why the bug is there. You should write your test to pass the code as is. Describe the bug in your bug tracker and move on. I’ve fixed bugs while writing tests only to find that the side effect breaks something else – but I had no tests so there was no way to know.

Start with the basics

The easiest way to get started is to write a test for every method, action and view that just runs without asserting anything. This will get your test coverage up. Running over the whole code base allows you to catch exceptions caused by typos. Doing this will also give you a good starting overview of the code base. Using a tool like ZenTest, or a recently posted rake task unspec’d, will help find everything to test.

Mocks and Stubs

Obviously you’ll be using mocks and stubs, but be careful not to stub out too much. Get a test in for every public method, and don’t stub out private methods.

Test your own code

Make sure that you write comprehensive tests for any code you add to the project. Never use the lack of existing tests as an excuse for not writing your own.

Any finally, don’t give up hope! For any reasonably complex project it is nearly always faster to implement the tests/specs than to rewrite the whole thing from scratch. Writing tests is actually a really good way to learn a foreign code base.

Tags: rspec, ruby, tdd, testing

This entry was posted on Monday, June 22nd, 2009 at 1:22 pm and is filed under Ruby on Rails. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

Leave a Reply

Click here to cancel reply.

CommentLuv Enabledshow more

  • Categories

    • Agile (3)
    • assessment (2)
    • consumer trends (8)
    • Cool tools (5)
    • Curriculum (2)
    • cybersafety (1)
    • Design (6)
    • Development (14)
    • devices (2)
    • Drupal (1)
    • e-Learning (7)
    • e-learning research network (1)
    • informal learning (5)
    • inquiry (4)
    • key competencies (6)
    • learning communities (3)
    • magic and delight (5)
    • Maori achievement (2)
    • multiliteracies (1)
    • professional learning (6)
    • Publishing (3)
    • Random thoughts (3)
    • research (7)
    • Ruby on Rails (8)
    • Sarah's top ten (11)
    • Social media (7)
    • Social software: practices (5)
    • social software: tools (9)
    • software (4)
    • Software for Learning website (4)
    • student work (7)
    • teacher-learner roles (5)
    • teaching practice (9)
    • the curriculum (6)
    • transformation (10)
    • Usabilty (3)
    • Writing (1)
  • Archives

    • August 2010 (4)
    • July 2010 (6)
    • June 2010 (2)
    • April 2010 (1)
    • March 2010 (1)
    • February 2010 (1)
    • January 2010 (3)
    • December 2009 (1)
    • November 2009 (1)
    • October 2009 (4)
    • September 2009 (2)
    • August 2009 (3)
    • July 2009 (6)
    • June 2009 (3)
    • May 2009 (1)
    • April 2009 (6)
    • March 2009 (6)
    • February 2009 (11)
    • December 2008 (4)
    • November 2008 (6)
    • October 2008 (12)
    • September 2008 (7)
    • August 2008 (7)
    • July 2008 (4)
  • Boost Loves Design

    • I love Typography
    • IntuitionHQ | easy website usability
    • OMG It even has a watermark
    • Follow me on Twitter
    © Boost Limited.
    All rights reserved.
    CONTACT US
    info@boost.co.nz
    tel. (04) 939 0062
    fax. (04) 939 0063

    Level 6, 175 Victoria Street
    PO Box 11504, Wellington
    New Zealand