Skip to content

Website: describe how to expect exceptions #169

Closed

Description

@BrainCrumbz

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

Activity

  1. amirrajan commented on Feb 25, 2017

    @amirrajan
    Collaborator

    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");
            };
        }
    }
  2. BrainCrumbz commented on Mar 6, 2017

    @BrainCrumbz
    CollaboratorAuthor

    Closed by b9e0ec3 with both descriptions

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions