.

Unit test an Exception

Testing an exception as follow:

[Test]
public void TestCorrectErrorTypeAndMessage()
{
    var ex = Assert.Throws<Exception>(
                    , () =>
                        {
                            DoThis();
                        });
    Assert.AreEqual("error123", ex.Message);
}

public void DoThis()
{
    throw new Exception("error123");
}

Another real example:

[Test]
public void MustHaveMetricsBeforeTakeSnapshot()
{
    var snapshotTaker = new SnapshotTaker();
        snapshotTaker.DataPoints = dataPoints;
        var ex = Assert.Throws<Exception>(() =>
                {
                    snapshotTaker.TakeSnapshot();
                });
    Assert.IsTrue(ex.Message.Contains("must provide"), "incorrect error message");
}

What's your thoughts on this?

*

Protected by WP Anti Spam