mardi 2 août 2016

My unit test is not catching an exception within my controller action

I have a unit test, something like:

[Test]
public void ThingController__Put__when_thing_is_invalid__then__throws()
{
    var controller = this.CreateThingController();

    try
    {
        var r = controller.Put("thing1", this.CreateInvalidThing());
    }
    catch(HttpResponseException hrex) when (hrex.Response.StatusCode == HttpStatusCode.BadRequest)
    {
        return; // implicit pass.
    }
    catch(Exception ex)
    {
        Assert.Fail($"Wrong exception {ex.GetType().Name}");
    }

    Assert.Fail("No exception thrown!");
}

But it always hits the last Fail, i.e. no exception is thrown. I have turned on first chance exceptions and can see it being thrown and is rethrown all the way up. It definitely is bubbling all the way up.

Aucun commentaire:

Enregistrer un commentaire