What block is not being tested in my test method? (VS08 Test Framework)

Posted by daft on Stack Overflow See other posts from Stack Overflow or by daft
Published on 2010-06-01T08:30:00Z Indexed on 2010/06/01 8:33 UTC
Read the original article Hit count: 206

I have the following code:

private void SetControlNumbers()
    {
        string controlString = "";
        int numberLength = PersonNummer.Length;

        switch (numberLength)
        {
            case (10) :
                controlString = PersonNummer.Substring(6, 4);
                break;
            case (11) :
                controlString = PersonNummer.Substring(7, 4);
                break;
            case (12) :
                controlString = PersonNummer.Substring(8, 4);
                break;
            case (13) : 
                controlString = PersonNummer.Substring(9, 4);
                break;
        }

        ControlNumbers = Convert.ToInt32(controlString);
    }

Which is tested using the following test methods:

[TestMethod()]
    public void SetControlNumbers_Length10()
    {
        string pNummer = "9999999999";
        Personnummer target = new Personnummer(pNummer);
        Assert.AreEqual(9999, target.ControlNumbers);
    }

    [TestMethod()]
    public void SetControlNumbers_Length11()
    {
        string pNummer = "999999-9999";
        Personnummer target = new Personnummer(pNummer);
        Assert.AreEqual(9999, target.ControlNumbers);
    }

    [TestMethod()]
    public void SetControlNumbers_Length12()
    {
        string pNummer = "199999999999";
        Personnummer target = new Personnummer(pNummer);
        Assert.AreEqual(9999, target.ControlNumbers);
    }

    [TestMethod()]
    public void SetControlNumbers_Length13()
    {
        string pNummer = "1999999-9999";
        Personnummer target = new Personnummer(pNummer);
        Assert.AreEqual(9999, target.ControlNumbers);
    }

For some reason Visual Studio says that I have 1 block that is not tested despite showing all code in the method under test in blue (ie. the code is covered in my unit tests). Is this because of the fact that I don't have a default value defined in the switch? When the SetControlNumbers() method is called, the string on which it operates have already been validated and checked to see that it conforms to the specification and that the various Substring calls in the switch will generate a string containing 4 chars. I'm just curious as to why it says there is 1 untested block. I'm no unit test guru at all, so I'd love some feedback on this.

Also, how can I improve on the conversion after the switch to make it safer other than adding a try-catch block and check for FormatExceptions and OverflowExceptions?

© Stack Overflow or respective owner

Related posts about c#

Related posts about .NET