Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Explicitly setting a test to pass/fail?

In the test below, if it enters the catch block I want to indicate that the test has passed. If the catch block is bypassed I want the test to fail.

Is there a way to do this, or am I missing the point with how tests should be structured?

[TestMethod]
public void CommandExecutionWillThrowExceptionIfUserDoesNotHaveEnoughEminence()
{
    IUserCommand cmd = CreateDummyCommand("TEST", 10, 10);
    IUser user = new User("chris", 40);

    try
    {
        cmd.Execute(user);
    }
    catch(UserCannotExecuteCommandException e)
    {
        //Test Passed
    }

    // Test Failed
}
like image 438
Chris Avatar asked Dec 07 '22 17:12

Chris


1 Answers

I tend to use this pattern when I have a similar situation:

// ...
catch (UserCannotExecuteCommandException e)
{
    return;    // Test Passed
}

Assert.Fail();    // Test Failed -- expected exception not thrown
like image 144
Cameron Avatar answered Dec 26 '22 20:12

Cameron