Skip to content
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

Provide a way of creating an instance of a service (not mock) only when it is being requested #196

Open
pkunze opened this issue Mar 14, 2023 · 2 comments

Comments

@pkunze
Copy link

pkunze commented Mar 14, 2023

We have a base class for all our Tests.
However, to minimize the overall runtime, we only instanciate those dependecies when they are actually needed.
This helped to greatly reduce the time it takes to execute all tests.

Now, I would like to use Automocker instead of ServiceCollection/ServiceProvider.

The only thing that is missing is a way to defer instanciation of dependencies.
My suggestion would be something along the lines of:

// current way
mocker.Use<IDriveTrain>(new DriveTrain());

// new overload
mocker.Use<IDriveTrain>(_ => new DriveTrain());

which would defer the Instanciation until eg var car = mocker.CreateInstance<Car>(); is called.

I think this is a pretty minor change and i'd gladly provide a PR for this, if you folks think that is not a terrible idea.

@Keboo
Copy link
Collaborator

Keboo commented Mar 14, 2023

This is technically possible today, albeit with a more complex API. The AutoMocker instance has a Resolvers property where you can pass in your own IMockResolver instance. Under the hood, most of the resolutions fall into these resolvers.

I suspect it would not be too difficult to have some sort of "DeferredInstanceResolver" that could contain those delegates and invokes them to get the instances.

If you would like to submit a PR for it, I would be happy to review it.

@pkunze
Copy link
Author

pkunze commented Mar 15, 2023

Awesome. Will do!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants