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

Handling Exceptions #12

Open
lacota opened this issue Jul 29, 2014 · 6 comments
Open

Handling Exceptions #12

lacota opened this issue Jul 29, 2014 · 6 comments
Projects

Comments

@lacota
Copy link

lacota commented Jul 29, 2014

How should we handle testing events or expected exceptions such as security or business rule violation?

@copypastedeveloper
Copy link
Owner

Thats a good question, There are actually helper functions for that. Here is an example of how to accomplish what you're looking for:

public class publishing_a_null_event : Scenario
{
    static Mock<IServiceBus> _bus;
    static Mock<ILog> _log;
    static EventPublisher _eventPublisher;
    static object _event;
    static Exception _exception;

    given an_event_publisher = () =>
    {
        _bus = new Mock<IServiceBus>();
        _log = new Mock<ILog>();
        _eventPublisher = new EventPublisher(_bus.Object,_log.Object);
    };

    given a_null_event = () => _event = null;

    when sending_an_event_to_the_publisher = () => _exception = Catch.Exception(() => _eventPublisher.PublishEvent(_event));

    [then]
    public void it_should_throw_an_argument_null_exception()
    {
        _exception.ShouldBeOfType<ArgumentNullException>();
    }

    [then]
    public void it_should_not_send_the_event_on_the_bus()
    {
        _bus.Verify(x => x.Publish(_event), Times.Never);
    }
}

The Catch.Exception helper method basically takes in any action and expects to catch an exception. If no exception is thrown it returns a DidNotThrowExceptionException. I've been contemplating changing that behavior, so if you have suggestions let me know.

@lacota
Copy link
Author

lacota commented Jul 29, 2014

Thanks Nathan, that works fine.

Is there any other documentation other than the readme?

Thanks

Robert

Quoting Nathan Gonzalez notifications@github.com:

Thats a good question, There are actually helper functions for that.
Here is an example of how to accomplish what you're looking for:

public class publishing_a_null_event : Scenario
{
    static Mock<IServiceBus> _bus;
    static Mock<ILog> _log;
    static EventPublisher _eventPublisher;
    static object _event;
    static Exception _exception;

    given an_event_publisher = () =>
    {
        _bus = new Mock<IServiceBus>();
        _log = new Mock<ILog>();
        _eventPublisher = new EventPublisher(_bus.Object,_log.Object);
    };

    given a_null_event = () => _event = null;

    when sending_an_event_to_the_publisher = () => _exception =  

Catch.Exception(() => _eventPublisher.PublishEvent(_event));

    [then]
    public void it_should_throw_an_argument_null_exception()
    {
        _exception.ShouldBeOfType<ArgumentNullException>();
    }

    [then]
    public void it_should_not_send_the_event_on_the_bus()
    {
        _bus.Verify(x => x.Publish(_event), Times.Never);
    }
}

The Catch.Exception helper method basically takes in any action and
expects to catch an exception. If no exception is thrown it returns
a DidNotThrowExceptionException. I've been contemplating changing
that behavior, so if you have suggestions let me know.


Reply to this email directly or view it on GitHub:
#12 (comment)

@copypastedeveloper
Copy link
Owner

Unfortunately not. There is a lot of hidden features. I've been meaning to dig back into this more lately as my time has been freeing up more to work on open source stuff. I'll add an issue for documentation and try and get started on it. I'm conflicted about whether to start by documenting some of the more hidden features or just the plain workings of it.

@copypastedeveloper
Copy link
Owner

there is this, i guess, from back when i released it last year...

http://thenathangonzalez.wordpress.com/2013/10/12/given-bdd-framework/

@lacota
Copy link
Author

lacota commented Jul 29, 2014

I would do a high level summary of the functionality so people have an
idea what can be accomplished and then drill down into specific use
cases. You've already covered the main use case in the readme so
visibility of the hidden features would be helpful.

Quoting Nathan Gonzalez notifications@github.com:

Unfortunately not. There is a lot of hidden features. I've been
meaning to dig back into this more lately as my time has been
freeing up more to work on open source stuff. I'll add an issue for
documentation and try and get started on it. I'm conflicted about
whether to start by documenting some of the more hidden features or
just the plain workings of it.


Reply to this email directly or view it on GitHub:
#12 (comment)

@copypastedeveloper
Copy link
Owner

sounds like a reasonable plan.

@copypastedeveloper copypastedeveloper added this to Todo in Thing Apr 3, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
No open projects
Thing
Todo
Development

No branches or pull requests

2 participants