reqnroll / Reqnroll

Open-source Cucumber-style BDD test automation framework for .NET.
https://reqnroll.net
BSD 3-Clause "New" or "Revised" License
302 stars 33 forks source link

Autofac plugion [BeforeTestRun] injection is using BoDi instead of Autofac global dependency #183

Open q42jaap opened 3 weeks ago

q42jaap commented 3 weeks ago

Reqnroll Version

2.0.3

Which test runner are you using?

NUnit

Test Runner Version Number

2.0.3

.NET Implementation

.NET 8.0

Test Execution Method

ReSharper Test Runner

Content of reqnroll.json configuration file

No response

Issue Description

I wanted to inject a global Autofac dependency into a [BeforeTestRun] static method. Autofac seems to be working fine, but a [BeforeTestRun] hook uses the TestRunContainer.

[BeforeTestRun]
public static void BeforeTestRun(IMyImpl myImpl) {}

Running the tests fail with the error below.

The docs do mention BeforeTestRun

Globally registered dependencies may be resolved in the [BeforeTestRun] and [AfterTestRun] methods.

Steps to Reproduce

  1. Create a testproject using the dotnet new reqnroll-project.
  2. Add Reqnroll.Autofac and add a SetupTestDependencies class with global and scenario attributes.
  3. Add a interface with an implementation to the Autofac ContainerBuilder.
  4. In the CalculatorStepDefinition (that's generated) add a dependency using the interface. This works with autofac
  5. Add a Binding (I called it SetupHook) with a BeforeTestRun static method that requests the interface.

The error that occurs:

  Failed AddTwoNumbers [290 ms]
  Error Message:
   OneTimeSetUp: Reqnroll.BoDi.ObjectContainerException : Interface cannot be resolved: MyRepro.Reqnroll.Autofac.IMyImpl

I traced it down to GetHookContainer in TestExecutionEngine, where the test run context has a ObjectContainer from BoDi.

Link to Repro Project

https://github.com/q42jaap/MyRepro.Reqnroll.Autofac

gasparnagy commented 3 weeks ago

I can confirm that the issue is reproducible. I am checking it.

Not related to the issue but a side note that the AddReqnrollBindings call must be configured in the [ScenarioDependencies]. But even after moving that to the other method, the issue is still there.

gasparnagy commented 3 weeks ago

I've got the fix.

There is also a workaround: You can depend on the IContainer in the hook method and resolve the object from there.