danielwertheim

danielwertheim


notes from a passionate developer

Share


Sections


Tags


Disclaimer

This is a personal blog. The opinions expressed here represent my own and not those of my employer, nor current or previous. All content is published "as is", without warranty of any kind and I don't take any responsibility and can't be liable for any claims, damages or other liabilities that might be caused by the content.

When will we ever see something like this in MSTest?

Perhaps I haven't looked enough but I really miss Assert.Throws in MSTest. Or does it exist? I've seen more people look for it.... For me, I just put together something real quick.

[DebuggerStepThrough()]
public static T Throws<T>(Action action) where T : Exception
{
    try
    {
        action.Invoke();
    }
    catch (T ex)
    {
        return ex;
    }
    catch (Exception ex)
    {
        throw new AssertFailedException(string.Format(
            "Expected exception was not thrown! Got: '{0}'.",
            ex.GetType()), ex);
    }

    throw new AssertFailedException(
        "Expected exception was not thrown! None was thrown.");
}

//Daniel

View Comments