mardi 28 juin 2016

Ensure that the method under test has thrown the expected exception, NOT any other part of the test case set up

I am using Visual Studio Unit Test Cases. I have written the Unit test case where Argument Exception is expected from the method under test MethodUnderTest. Suppose if any other part of the test case (Setup part) throws the expected exception ArgumentException, Then I want to enforce that my test case should fail. It should pass only in case Setup is correct and instance.MethodUnderTest(); line of code throws ArgumentException.

I can achieve using try catch, but I want to know is there any better approach to achieve this.

[ExpectedException(typeof(ArgumentException))]
public void TestCaseMethod()
{        
    // Set up
    Mock<ITestClass> testM = new Mock<ITestClass>();
    AnimalClass instance = new AnimalClass(testM.Object);

    // call the method under test
    instance.MethodUnderTest();
}

Aucun commentaire:

Enregistrer un commentaire