1

Using Specflow's basic dependency injection, it is possible to inject a dynamic context object as below.

This saves having to use the dictionary which I find clunky, are there drawbacks to this approach?

public class TestClass
{
    private readonly dynamic context;

    public TestClass(ExpandoObject context)
    {
        this.context = context;
    }

    [Given(@"I have a file")]
    public void GivenIHaveAFile()
    {
        context.FilePath = @".\Folder\File";
    }

    [When(@"I use the file")]
    public void GivenIHaveAFile()
    {
        var fileContents = File.ReadAllText(context.FilePath);
    }
}

1 Answer 1

3

The drawbacks are only those that you get with using dynamic objects in general. You'll get no intellisense and won't be able to add any behaviour to your object.

Personally I prefer to use specific classes to handle passing state between steps as these objects can then be assigned behaviour as well as holding data, but YMMV.

In place of the generic dictionary though this seems reasonable, as it at least avoids the casting.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.