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

Adding a step before transition #254

Open
SeanFarrow opened this issue Jun 20, 2017 · 6 comments
Open

Adding a step before transition #254

SeanFarrow opened this issue Jun 20, 2017 · 6 comments

Comments

@SeanFarrow
Copy link

Hi,

I'm migrating code from JustBehave and would like to add a step to run before any transition steps (it's not really a given as it creates the class under test.) Is there anyway of doing this without modifying the ExecutionOrder enum? Creating the class under test in a given step just seems wrong to me!
Alternativdely, if I make the changes, would this be accepted as a PR?

@mwhelan
Copy link
Member

mwhelan commented Jun 20, 2017

Hi @SeanFarrow . Yes, if you have a method named 'Setup,' or ending with 'Context,' it will run before any transition methods, and can be a good place to create the SUT. These rules are codified in the DefaultMethodNameStepScanner. You could potentially replace this scanner if you preferred other method names.

@SeanFarrow
Copy link
Author

SeanFarrow commented Jun 20, 2017 via email

@mwhelan
Copy link
Member

mwhelan commented Jun 20, 2017

No, Setup or *Context will run before Given When Then Transitions, then TearDown will run afterwards. There's more info in the docs here.

I'd be happy to take a look at some of your JBehave tests and help you get the syntax right for the port to BDDfy, if you have a public repo you could point me at.

@SeanFarrow
Copy link
Author

SeanFarrow commented Jun 20, 2017 via email

@mwhelan
Copy link
Member

mwhelan commented Jun 20, 2017

Ah OK, when you said transitions, you meant the When step? I thought you were referring to all the GWT steps but just the When would make sense. Yes, I think this can be done, but it would really help to see an example, even if it's just a made up one.

How is it done in JBehave? i.e. What is the functionality of that framework that you are trying to port over?

I would be interested to know how 'automatic' you want to make this? If you are just going to have it be a method of every class, then you could just make the final AndGiven method create the SUT, or you could have a When method create the SUT, and an AndWhen method for your When. Neither very elegant, and both would be reported, which I suspect is what you are wanting to avoid?

Personally, I like to have a SUT variable that lazily creates the SUT the first time it is called from your scenario. Something like this:

public abstract class ScenarioFor<T>
{
    private T _sut;

    public T SUT
    {
        get
        {
            if (_sut == null)
            {
                _sut = CreateSut<T>();
            }
            return _sut;
        }
    }

    protected virtual T CreateSut<T>()
    {
        return default(T);
    }
}

You can override the CreateSut method in each test or you can implement this method to automatically create the SUT from an IoC container or an automocking container.

But yeah, it would be good to have an example as there are a number of things we could do.

@SeanFarrow
Copy link
Author

SeanFarrow commented Jun 29, 2017 via email

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

2 participants