Skip to content

Register mock/service as scoped #188

@archigo

Description

@archigo

We have a Parallel.ForEach that uses IServiceScopeFactory to make sure certain services are different instances.

I am having a hard time figure out how to make this work when resolving through automocker.

I can mock the scope resolution to resolve to an automocker, but I can not seem to find a way to make that scoped automocker have the mocks that are set on the original automocker.

Our setup is that we have a base class that all our tests inherit from. The base class will set up an automocker with default configs and we add to those configs in each test.

Is there a convenient way to work with scoped services in automocker, or a way to copy the setup in an existing automocker to a new mocker?

            // the base mocker that all tests use
            autoMocker = CreateDefaultAutoMocker();

            var scopeMock = autoMocker.GetMock<IServiceScope>();
            scopeMock.Setup(x => x.ServiceProvider).Returns(() =>
            {
                var scopedMocker = CreateDefaultAutoMocker();
                // copy all setup from base mocker here and overwrite the scoped service?
                scopedMocker.Use<IScopedService>(new ScopedService());
                return scopedMocker; 
            });

            var scopeFacMock = autoMocker.GetMock<IServiceScopeFactory>();
            scopeFacMock.Setup(x => x.CreateScope()).Returns(scopeMock.Object);

In the above code (from our base setup class) the scopedMocker is missing the mock setup from individual tests

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions