Skip to content

Add some support for making assertions expecting exceptions #21

Description

@EamonNerbonne

From stephen.swensen on January 11, 2012 21:44:44

Something similar to Unquote's "raises" operator: https://code.google.com/p/unquote/wiki/GettingStarted . i.e. the assertion fails if the excepted exception (a generic argument) is not a subclass of the actual expression, or indeed if no exception was thrown at all.

Somewhat related to issue #20 (you want to print as much known / evaluated information about the expression as possible before the actual but not expected exception is thrown or otherwise print everything when no exception thrown).

Original issue: http://code.google.com/p/expressiontocode/issues/detail?id=21

Activity

  1. EamonNerbonne commented on May 24, 2014

    @EamonNerbonne
    OwnerAuthor

    From eamon.nerbonne on March 07, 2013 06:09:34

    Well, this is intended to be used as part of a unit testing framework, not as a replacement: and NUnit and xUnit.NET both have a method Assert.Throws which probably does what you want. I mean, I could reimplement that here, but I'm not sure of when that would be useful. How are you using this?

    Labels: -Type-Defect Type-Enhancement

  2. EamonNerbonne commented on May 24, 2014

    @EamonNerbonne
    OwnerAuthor

    From eamon.nerbonne on March 07, 2013 06:09:52

    Owner: eamon.nerbonne

  3. EamonNerbonne commented on May 24, 2014

    @EamonNerbonne
    OwnerAuthor

    From stephen.swensen on March 07, 2013 08:42:36

    The idea is the same as for PAssert.That: you should be able to provide more info about the expression that is expected to throw an exception but doesn't.

    For example, you could have:

    PAssert.Throws(() => DoSomething("asdf"));

    and if the expression doesn't raise an exception, fail with a message like:

    Excepted 'System.Exception' but no exception was thrown:

    DoSomething("asdf")
    |
    |
    4

    The built-in NUnit and xUnit.NET Assert.Throws methods on-the-other-hand only tell you whether or not the expected exception was thrown, without any runtime info about the expression.

  4. EamonNerbonne commented on May 24, 2014

    @EamonNerbonne
    OwnerAuthor

    From stephen.swensen on March 07, 2013 08:57:39

    Also note that it would be a good idea to return the exception that was thrown from PAssert.Throws, so that it may be used for further assertions if the user wants to make further assertions on properties of the exception.

    Or, do something like unquote does, and have a method like PAssert.Throws(() => DoSomething("asdf"), exn => exn.Message == "bad input!"), and display something like the following if the exceptions IS thrown, but the follow-up assertion on the exception fails:

    The expected exception 'System.Exception' was thrown, but the follow-up assertion failed:

    exn.Message == "bad input!"
    |
    "BAD INPUT!"

    The exception throwing expression was:

    DoSomething("asdf")
    |
    System.Exception was thrown

  5. removed their assignment
    on Mar 29, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions