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

An exception in OnAssemblyStart should stop the execution of the specs in the assembly #236

Open
ulrichb opened this issue Jun 21, 2014 · 4 comments
Milestone

Comments

@ulrichb
Copy link
Contributor

ulrichb commented Jun 21, 2014

... to avoid consequential errors in the specs.

The following code sample demonstrates the issue: although the first exception is displayed in the R# and console runner, the execution continues and throws a NullReferenceException within When_using_the_context_value as a consequence of the exception in the AssemblyContext. The current behavior is more annoying if the relationship between the assembly context and the specs is more indirect (like creating a directory in the assembly context and use that in the specs or setting up some test data in a database in the assembly context) because then it's harder to detect that the specs just fail because of an error in the assembly context.

Further, NUnit's [SetUpFixture] and xUnit.net's IUseFixture<> also stop the execution of the concerned tests.

public class AssemblyContext : IAssemblyContext
{
    public static SomeContext SomeContext;

    public void OnAssemblyStart()
    {
        // SomeContext will stay null because the c'tor throws:
        SomeContext = new SomeContext();
    }

    public void OnAssemblyComplete() { }
}

public class SomeContext
{
    public SomeContext()
    {
        throw new Exception();
    }

    public object Value { get { return new object(); } }
}

class SomeSpec
{
    class When_using_the_context_value
    {
        Because of = () =>
        {
            // This will throw a NullReferenceException because 
            // AssemblyContext.SomeContext == null:
            Console.WriteLine(AssemblyContext.SomeContext.Value);
        };

        It happens_something = () => { };
    }
}
@danielmarbach danielmarbach added this to the 0.8.3 milestone Jun 23, 2014
@danielmarbach danielmarbach added feature and removed bug labels Jun 23, 2014
@danielmarbach danielmarbach removed this from the 0.8.3 milestone Jun 23, 2014
@danielmarbach
Copy link
Contributor

I just ran several tests on this. An exception with the full stack trace is raised inside resharper. See for example:

2014_06_23_22_09_01_unit_test_runner

After you click OK all the specs are executed nonetheless and the mentioned NullReferenceException is raised. This is exactly the same behavior as when you have a specification which has an exception in the Establish clause.

In order to fix this we need to make IAssemblyContext a first class citizen in the framework which is a major task. As mentioned because the behavior is the same as in the establish clause I consider this works as intented. I leave it in the backlog for future reference.

@danielmarbach
Copy link
Contributor

I'll be more than happy to review pull requests if this is a major show stopper for you.

@ulrichb
Copy link
Contributor Author

ulrichb commented Jun 25, 2014

This is exactly the same behavior as when you have a specification which has an exception in the Establish clause.

In the following example, when executing it in the R# or console runner, you will see the Exception thrown in the Establish step and no consequential NullReferenceException because the execution of the scenario stops immediately (same behavior, if the Establish is moved into When_using_the_context_value).

public class SomeContext
{
    public SomeContext()
    {
        throw new Exception();
    }

    public object Value { get { return new object(); } }
}

class SomeSpec
{
    static SomeContext SomeContext;

    Establish ctx = () =>
    {
        SomeContext = new SomeContext();
    };

    class When_using_the_context_value
    {
        Because of = () =>
        {
            Console.WriteLine(SomeContext.Value);
        };

        It happens_something = () => { };
    }
}

@drauch
Copy link
Contributor

drauch commented Jun 8, 2015

Any updated on this?

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

No branches or pull requests

5 participants