Home
Manage Your Code
Snippet: TDD - Expecting a Exception in a Fixture (C#)
Title: TDD - Expecting a Exception in a Fixture Language: C#
Description: Simple helper class that can be used to return (instead of throwing) a exception object when a exception is expected in a test fixture. Views: 194
Author: Schalk van Wyk Date Added: 8/30/2010
Copy Code  
1public class Catch
2{
3    public static Exception Exception(Action context)
4    {
5        try
6        {
7            context();
8        }
9        catch (Exception thrownException)
10        {
11            return thrownException;
12        }
13
14        return null;
15    }
16}
Usage
Exception exception = Catch.Exception(() => [A action that will cause a eception]);
exception.ShouldBeOfType<[Some Specific Exception]>();