danielwertheim

danielwertheim


notes from a passionate developer

Developer that lives by the mantra "code is meant to be shared".

Share


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?

Daniel WertheimDaniel Wertheim

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

Developer that lives by the mantra "code is meant to be shared".

Comments