solidtoken / SpecFlow.DependencyInjection

SpecFlow plugin that enables to use Microsoft.Extensions.DependencyInjection for resolving test dependencies.
BSD 3-Clause "New" or "Revised" License
35 stars 11 forks source link

How to use TestRunContext for inject configuration #78

Closed arnaulddumez closed 2 years ago

arnaulddumez commented 2 years ago

There are difference with default SpecFlow Ioc, the method is static and is not possible to inject TestRunContext like the Specflow sample.


    [Binding]
    public class Hooks
    {
        private readonly TestRunContext _testRunContext;

        public Hooks(TestRunContext testRunContext)
        {
            _testRunContext = testRunContext;
        }

        [BeforeScenario(Order = 1)]
        public void RegisterDependencies(IObjectContainer objectContainer)
        {
            objectContainer.RegisterInstanceAs(new DatabaseContext());

            IConfiguration config = new ConfigurationBuilder()
                .AddJsonFile(Path.Combine(_testRunContext.TestDirectory, "appsettings.json"), optional: true, reloadOnChange: true)
                .Build();

            objectContainer.RegisterInstanceAs(config);

VS

    public static class ServiceModule
    {
        [ScenarioDependencies]
        public static IServiceCollection CreateServices()
        {
            var services = new ServiceCollection();

There are equivalent please ?

mbhoek commented 2 years ago

Hello @arnaulddumez, thanks for your question. Your example (injecting a context class using the constructor) should work using this plugin, please refer to this included test for an example: https://github.com/solidtoken/SpecFlow.DependencyInjection/blob/main/SpecFlow.DependencyInjection.Tests/DependencyInjectionPluginSteps.cs (the dependencies are registered here: https://github.com/solidtoken/SpecFlow.DependencyInjection/blob/main/SpecFlow.DependencyInjection.Tests/TestDependencies.cs).

I am not familiar with TestRunContext -- the only reference I could find is in the SpecFlow+ Runner API documentation (https://docs.specflow.org/projects/specflow-runner/en/latest/Usage/SpecFlow-Runner-APIs.html). Recently Tricentis announced the retirement of SpecFlow+ Runner (https://specflow.org/using-specflow/the-retirement-of-specflow-runner/) so I'm not keen on spending time on it.