Welcome to mirror list, hosted at ThFree Co, Russian Federation.

ExceptionTestCase.cs « NUnitCore « src « nunit « mcs - github.com/mono/mono.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: a5ae174fd613b0a06c6fff50a64bdeec89327534 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
namespace NUnit.Extensions 
{
	using System;
	using NUnit.Framework;

	/// <summary>A TestCase that expects an Exception of class fExpected
	/// to be thrown.</summary>
	/// <remarks> The other way to check that an expected exception is thrown is:
	/// <code>
	/// try {
	///   ShouldThrow();
	/// }catch (SpecialException) {
	///   return;
	/// }
	/// Assertion.Fail("Expected SpecialException");
	/// </code>
	///
	/// To use ExceptionTestCase, create a TestCase like:
	/// <code>
	/// new ExceptionTestCase("TestShouldThrow", typeof(SpecialException));
	/// </code></remarks>
	public class ExceptionTestCase: TestCase 
	{
		private readonly Type fExpected;
		/// <summary>
		/// 
		/// </summary>
		/// <param name="name"></param>
		/// <param name="exception"></param>
		public ExceptionTestCase(string name, Type exception) : base(name) 
		{
			fExpected= exception;
		}

		/// <summary>Execute the test method expecting that an Exception of
		/// class fExpected or one of its subclasses will be thrown.</summary>
		protected override void RunTest() 
		{
			try 
			{
				base.RunTest();
			}
			catch (Exception e) 
			{
				if (fExpected.IsAssignableFrom(e.InnerException.GetType()))
					return;
				else
					throw e.InnerException;
			}
			Assertion.Fail("Expected exception " + fExpected);
		}
	}
}