Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Website: describe how to expect exceptions #169

Closed
BrainCrumbz opened this issue Feb 25, 2017 · 2 comments
Closed

Website: describe how to expect exceptions #169

BrainCrumbz opened this issue Feb 25, 2017 · 2 comments

Comments

@BrainCrumbz
Copy link
Collaborator

Website makes no mention of how to assert when examples should throw, by using expect<>

@amirrajan
Copy link
Collaborator

amirrajan commented Feb 25, 2017

That should be documented, and also this: #59

class describe_something : nspec
{
    SomeObject someObject;
    void a_context()
    {
        expect_an_exception(
            "it throws an exception", 
            () => someObject.DoSomething(), 
            ex => someObject.Flag.should_be(false));

        expect_an_exception(
            "another test",
            () => someObject.DoSomethingElse(), 
            ex => someObject.Flag.should_be(false));
    }

    void expect_an_exception(string testName, Action actionThatThrows, Action<Exception> assertAfter)
    {
        it[testName] = () => 
        {
            bool exceptionThrown = false;
            try
            {
                actionThatThrows();
            }
            catch(Exception ex)
            {
                exceptionThrown = true;
                assertAfter(ex);
            }

            if(!exceptionThrown) throw new InvalidOperationException("exception was not thrown when expected");
        };
    }
}

@BrainCrumbz
Copy link
Collaborator Author

Closed by b9e0ec3 with both descriptions

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants