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: 8af9414d4fe40835ce64fdf26c02b02b6074ec81 (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
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;
  /// }
  /// Fail("Expected SpecialException");
  /// </code>
  ///
  /// To use ExceptionTestCase, create a TestCase like:
  /// <code>
  /// new ExceptionTestCase("TestShouldThrow", typeof(SpecialException));
  /// </code></remarks>
  public class ExceptionTestCase: TestCase {

    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;
      }
      Fail("Expected exception " + fExpected);
    }
  }
}