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

SUT needs to be reset for Examples #29

Open
mwhelan opened this issue Feb 4, 2018 · 0 comments
Open

SUT needs to be reset for Examples #29

mwhelan opened this issue Feb 4, 2018 · 0 comments

Comments

@mwhelan
Copy link
Owner

mwhelan commented Feb 4, 2018

Specify currently creates the SUT and container per scenario, but needs to do so per test case when examples are used. This causes an issue when subsequent test cases attempt to set dependencies in the container. An exception is thrown because the SUT has been set. This is expected behaviour for a scenario but needs to be enhanced to handle the situation with test cases.

public class ExampleScenario : ScenarioFor<ConcreteObjectWithOneInterfaceConstructor>
{
    private int _result;
    private IDependency1 _dependency;

    public ExampleScenario()
    {
        Examples = new ExampleTable("Num", "Result")
        {
            {1, 1},
            {2, 2}
        };
    }
    public void GivenTheValue(int num)
    {
        _dependency = new Dependency1 { Value = num };
        Container.Set<IDependency1>(_dependency);
    }
    public void WhenICheckTheDependencyValue()
    {
        _result = SUT.Dependency1.Value;
    }
    public void ThenItShouldBe_(int result)
    {
        _result.ShouldBe(result);
    }
}

Specify resolves each scenario from the IoC container and provides a fresh child container per scenario. The constructor runs once. Then, for each Example test case, the BDDfy methods run (Given When Then etc.). This means each test case is sharing the SUT and container. This will be fixed in the next release of Specify, but a workaround for the moment is to reset the SUT for each Example test case. This can be achieved by adding a Setup method.

public class ExampleScenario : ScenarioFor<ConcreteObjectWithOneInterfaceConstructor>
{
    private int _result;
    private IDependency1 _dependency;

    public ExampleScenario()
    {
        Examples = new ExampleTable("Num", "Result")
        {
            {1, 1},
            {2, 2}
        };
    }
    public void Setup()
    {
        SUT = null;
    }
    public void GivenTheValue(int num)
    {
        _dependency = new Dependency1 { Value = num };
        Container.Set<IDependency1>(_dependency);
    }
    public void WhenICheckTheDependencyValue()
    {
        _result = SUT.Dependency1.Value;
    }
    public void ThenItShouldBe_(int result)
    {
        _result.ShouldBe(result);
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant