-
-
Notifications
You must be signed in to change notification settings - Fork 53
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Register mock/service as scoped #188
Comments
@archigo I think something like this is closer to what you want. [TestMethod]
public void ScopedItemsAreResolvedWithinScopes()
{
AutoMocker mocker = new();
var scopeFacMock = mocker.GetMock<IServiceScopeFactory>();
scopeFacMock.Setup(x => x.CreateScope()).Returns(() => {
AutoMocker scopedMocker = new();
//Regsiter items that should be scoped
scopedMocker.With<IScopedService, ScopedService>();
Mock<IServiceScope> scope = new();
scope.Setup(x => x.ServiceProvider).Returns(() => scopedMocker);
return scope.Object;
});
using var scope1 = mocker.CreateScope();
using var scope2 = mocker.CreateScope();
IScopedService instance1 = scope1.ServiceProvider.GetRequiredService<IScopedService>();
IScopedService instance2 = scope2.ServiceProvider.GetRequiredService<IScopedService>();
Assert.AreNotSame(instance1, instance2);
} It is also worth noting that |
The issue is that there are mocks set up that needs to be available to on the scoped mocks as well. I am resolving a service which is not concurrency safe, so it is important that a new service is resolved in each scope, but I still need to have the mocks that are returning external data in each scope as the service depends on them. public class BaseTestClass {
protected baseAutoMocker;
public BaseTestClass(){
baseAutoMocker= new AutoMocker();
// setup mocks for all the things that always needs to be setup, like auth services and other stuff.
....
var scopeFacMock = baseAutoMocker.GetMock<IServiceScopeFactory>();
scopeFacMock.Setup(x => x.CreateScope()).Returns(() => {
AutoMocker scopedMocker = new(); // this somehow needs to copy the mock that each unit test adds to baseAutoMocker
//Register items that should be scoped
scopedMocker.With<IScopedService, ScopedService>();
Mock<IServiceScope> scope = new();
scope.Setup(x => x.ServiceProvider).Returns(() => scopedMocker);
return scope.Object;
});
}
} [TestClass]
public class Tests : BaseTestClass {
[TestMethod]
public void SomeTest()
{
var someServiceMock = baseAutoMocker.GetMock<SomeService>();
someServiceMock.Setup(...) // this needs to be setup on all of the scoped mocks as well!
var sut = baseAutoMocker.CreateInstance<ServiceToBeTested>();
sut.Run()
Assert...
}
} // code to be tested
Parallel.ForEach(operations, operation =>
{
var sp = _serviceScopeFactory.CreateScope().ServiceProvider;
var dbcontext = sp.GetRequiredService<DbContext>();
var someEntity = dbcontext.SomeEntities.First(x => x.Id == operation.Id);
var someService = sp.GetRequiredService<SomeService>();
// get some mocked data from someService to create some database chages
var data = someService.GetData();
someEntity.Something = data.Something;
...
dbcontext .SaveChanges();
}); |
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?
In the above code (from our base setup class) the scopedMocker is missing the mock setup from individual tests
The text was updated successfully, but these errors were encountered: