Created
June 19, 2012 02:26
-
-
Save pinzolo/2951970 to your computer and use it in GitHub Desktop.
例外発生検査メソッド
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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