copypastedeveloper / given

Given is a bdd library made with the intent of wrapping common testing frameworks easily.
10 stars 2 forks source link

Handling Exceptions #12

Open lacota opened 10 years ago

lacota commented 10 years ago

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

copypastedeveloper commented 10 years ago

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 commented 10 years ago

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: https://github.com/ncgonz/given/issues/12#issuecomment-50500376

copypastedeveloper commented 10 years ago

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 commented 10 years ago

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

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

lacota commented 10 years ago

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: https://github.com/ncgonz/given/issues/12#issuecomment-50509126

copypastedeveloper commented 10 years ago

sounds like a reasonable plan.