Skip to content

Feature: Capture and rethrow errors into the "it"s #201

@BennieCopeland

Description

@BennieCopeland

It would be nice to be able to do something like the following.

context["do something"] = () =>
{
    int someValue = int.MaxValue;
    MyClass myClass = null;

    beforeEach = () =>
    {
        myClass = new MyClass();
    };

    act = () => myClass.DoSomething(someValue);

    context["happy: value is good"] = () =>
    {
        beforeEach = () =>
        {
            someValue = 1;
        };

        it["will be happy"] = () =>
        {
            myClass.someValue.Should().Be(1);
        };
    };

    context["sad: value is bad"] = () =>
    {
        beforeEach = () =>
        {
            someValue = 256;
        };

        it["will throw an exception"] = expect<ArgumentOutOfRangeException>((exception) =>
        {
            exception.Message.Should().Be("You're an idiot!");
        });
    };
};

For singular cases, maybe something like this:

it["will throw an exception"] = expect<Exception>(() =>
{
    throw new Exception();
},
(exception) =>
{
    exception.Message.Should().Be("You're an idiot!");
});

I'm currently doing the following as a workaround, but a built in feature like above would be nice.

context["do something"] = () =>
{
    int someValue = int.MaxValue;
    MyClass myClass = null;
    bool expectException = false;
    Action action = null;

    beforeEach = () =>
    {
        myClass = new MyClass();
        expectException = false; // have to always set or it will screw up other contexts
    };

    act = () =>
    {
        action = () => myClass.DoSomething(someValue);
        if (!expectException) action();
    };

    context["sad: value is bad"] = () =>
    {
        beforeEach = () =>
        {
            someValue = 256;
        };

        it["will throw an exception"] = () =>
        {
            try
            {
                action();
            }
            catch(ArgumentOutOfRangeException ex)
            {
                ex.Message.Should().Be("You're an idiot!");
            }
        };
    };
};

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions