Skip to content

Instantly share code, notes, and snippets.

@pinzolo
Created June 19, 2012 02:26
Show Gist options
  • Save pinzolo/2951970 to your computer and use it in GitHub Desktop.
Save pinzolo/2951970 to your computer and use it in GitHub Desktop.
例外発生検査メソッド
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace MktSys.Test
{
/// <summary>
/// 独自 Assert クラス
/// </summary>
public static class AssertEx
{
/// <summary>
/// 指定の例外が発生することを検証する。
/// </summary>
/// <typeparam name="T">例外型</typeparam>
/// <param name="action">実行処理</param>
/// <returns>発生した例外インスタンス</returns>
/// <remarks>MSTest には存在しないので、NUnit から一部だけ輸入</remarks>
public static T Throws<T>(Action action) where T : Exception
{
T caught = null;
try
{
action();
Assert.Fail("例外が発生しませんでした。");
}
catch (T ex)
{
caught = ex;
}
Assert.AreEqual(caught.GetType(), typeof(T));
return caught;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment