0

We are working on creating a suite of automated playwright tests for our app through specflow. I have been trying to come up with a way to use specflow hooks to seed data before/clean up data after each test so that we can ensure that no tests are interfering with each other. Elsewhere in our program, functions are able to receive the DbContextFactory through dependency injection:

        public async Task<ClientPayload> AddClientAsync(AddClientInput input, [Service] IDbContextFactory<ProjectDbContext> dbContextFactory, [Service] IMapper mapper, CancellationToken cancellationToken)
        {
            return new(await this.AddEntityAsync<AddClientInput, Client>(input, dbContextFactory, mapper, cancellationToken));
        }

But trying to do this in a specflow hooks file like this:

    public class ActivitiesHooks
    {
        private readonly IDbContextFactory<ProjectDbContext> myDbContextFactory;
        public ActivitiesHooks([Service] IDbContextFactory<ProjectDbContext> _myDbContextFactory)
        {
            this.myDbContextFactory = _myDbContextFactory;
        }

        [BeforeScenario, Scope(Scenario = "Editing an Activity")]
        public void BeforeEditingAnActivity()
        {
            using var dbContext = ProjectDbContextFactory.CreateDbContext();
            var activityToUpdate = new Activity { ... };
            dbContext.Set<Activity>().Add(activityToUpdate);
            dbContext.SaveChanges();
        }

generates a strange error when I try to run the actual test (It builds just fine):

  Error Message:
   BoDi.ObjectContainerException : Interface cannot be resolved: Microsoft.EntityFrameworkCore.IDbContextFactory`1[[Project.Framework.EntityFramework.ProjectDbContext, Project.Framework, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]] (resolution path: Project.Specs.Hooks.ActivitiesHooks)
TearDown : BoDi.ObjectContainerException : Interface cannot be resolved: Microsoft.EntityFrameworkCore.IDbContextFactory`1[[Project.Framework.EntityFramework.ProjectDbContext, Project.Framework, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]] (resolution path: Project.Specs.Hooks.ActivitiesHooks)

The hooks file is not using BoDi, although it is in use elsewhere in the code, so I'm not sure why that's included in the error. I've also tried inject just the DbContext, not the DbContextFactory, and that generates an identical error. Is there a fix for this, or is there a better way to use DbContext to seed data for tests?

Rain Code
  • 1
  • 2
  • Why not make this code a `Given` step in each scenario? This looks like information that belongs in the scenario in order to fully understand it. Remember that with Behavior-Driven Development, code re-use is not the primary goal for a scenario. They should be understandable by end users. – Greg Burghardt May 26 '22 at 13:16
  • You probably need to register the db context with the DI container rather than passing it in via the constructor. – Greg Burghardt May 31 '22 at 15:51

0 Answers0