Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Test driven development - when/what to test?

  • 19-04-2013 12:40pm
    #1
    Registered Users, Registered Users 2 Posts: 5


    I am trying to get started with TDD but right away I am unsure of when and what I should be testing. The first two tasks in a new project I'm working on are as follows -

    1) Create a REST endpoint that receives some JSON formatted data on a saves it to the database. Say the data was several car records -
    {
        "cars": [{
                "make": "ford",
                "color": "blue",
                "year": "2010",
                "for_sale": true
            }, {
                "make": "bmw",
                "color": "black",
                "year": "2011",
                "for_sale": false
            }
        ]
    }
    

    So that data arrives at a REST endpoint and I need to save it in the database. Do I need a test (or a number of tests) for this task and if so what should it look like? How should I approach the job of testing this task, do I write out a list of things that should be tested...if so, what would such a list look like?

    2) Retrieve some records from the database and display them in a view/webpage (i.e. using some templating system). Say the records are the car records above and they should be displayed as follows -

    [HTML]<ul id="cars">
    <li id="car-1">
    <div><span>Make:</span><span>Ford</span>
    </div>
    <div><span>Color:</span><span>blue</span>
    </div>
    <div><span>Year:</span><span>2010</span>
    </div>
    <div><span>For sale:</span><span>Yes</span>
    </div>
    </li>
    <li id="car-2">
    <div><span>Make:</span><span>BMW</span>
    </div>
    <div><span>Color:</span><span>black</span>
    </div>
    <div><span>Year:</span><span>2011</span>
    </div>
    <div><span>For sale:</span><span>No</span>
    </div>
    </li>
    </ul>[/HTML]

    So do I need a test for this task and if so what should it look like?


Comments

  • Registered Users, Registered Users 2 Posts: 2,793 ✭✭✭John_Mc


    The 3 scenarios are:

    1) It fails
    2) It succeeds
    3) There's an exception

    With TDD you need to know the inputs and the desired outputs. In this case, the output is a call to the data access layer to add your record.

    In the second method, you are testing that the DAL returns records for the given parameters.

    Your tests for handling failures and exceptions should be testing the scenarios where something wrong has happened. Like input parameters being empty or in the incorrect format.


  • Moderators, Society & Culture Moderators Posts: 9,689 Mod ✭✭✭✭stevenmu


    You should read up a bit about unit testing, which in very very shorthand is really just creating tests for individual units of functionality in your project.

    For the first task, you need to create a unit test that can call an your REST endpoint, passing in the JSON data. Either this test will also test that the data makes it through to the DB, or it will just the response from the endpoint and you would write another test to check that it goes through to the DB ok.

    Then when your unit tests are ready, you write the code for your REST endpoint to pass the unit tests.


  • Registered Users, Registered Users 2 Posts: 2,793 ✭✭✭John_Mc


    If you're using .Net and Visual Studio then I'd suggest installing the trial version of NCrunch.

    It's a Unit Test runner which graphically displays the lines covered and not covered by your unit tests. Very helpful and should give you a boost with the learning curve as it runs the tests each time they are changed (without you having to tell Visual studio to compile and run them).


Advertisement