Skip to content

Instantly share code, notes, and snippets.

@gregoryyoung
Created July 4, 2011 21:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save gregoryyoung/1063974 to your computer and use it in GitHub Desktop.
Save gregoryyoung/1063974 to your computer and use it in GitHub Desktop.
Failing spec
/*
* Sometimes the point of a specification is to show that a given scenario fails.
*
* A failing specification is similar to a SUT specification. It will return the SUT
* from its given method. The SUT will then be passed to the when() method where some
* action will happen. It is expected that this action will throw an exception.
*
* The exception is then passed to the expectations.
*
* note: if you are doing more complex operations other templates can be used with
* the TestContext pattern.
*/
public class FailingSpecification
{
public ISpecification it_will_fail = new FailingSpecification<FailingExample, SomethingFailedException>()
{
On = () => new FailingExample(),
When = (obj) => obj.CauseFailure(),
Expect =
{
exception => exception.Message == "Something failed!",
exception => exception.ErrorCode == 17
},
};
}
public class FailingExample
{
public void CauseFailure()
{
throw new SomethingFailedException("Something failed!", 17);
}
}
public class SomethingFailedException : Exception
{
public readonly int ErrorCode;
public SomethingFailedException(string msg, int errorCode) : base(msg)
{
ErrorCode = errorCode;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment