-
-
Notifications
You must be signed in to change notification settings - Fork 109
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
Deeper integration with mocking frameworks #310
Comments
Hi @egil, i was looking into it. And i would like to discuss my suggestion before cleaning up etc..
My plan is to alter this function with a Func like this:
Was wondering what your opinion on it is? |
Thanks @thopdev. I'm not at home right now, but will respond in detail later. |
We are not too far apart in our thinking of this @thopdev, however, I have a slight change to the order of how things happens. To AddFallbackServiceProvider(IServiceProvider fallbackServiceProvider); Then, the public object GetService(Type serviceType)
{
if (_serviceProvider is null)
_serviceProvider = _serviceCollection.BuildServiceProvider();
var result = _serviceProvider.GetService(serviceType);
if (result is null && fallbackServiceProvider is not null)
result = fallbackServiceProvider.BuildServiceProvider();
return result;
} So why not call the fallback first? The idea is that 3rd party component library venders can have an e.g. If the fallback is called first, then any services already registered that might not need to be mocked, will be, if the fallback is something like Moq or NSubstitute. What do you think? |
Now i think about about the order, your right. The user should always be allowed to override the fallback. When i get time i'll start working on a PR. Not sure if i can do it today as i'm streaming tonight. But maybe i get some time before otherwise tomorrow :) |
Sounds good (where do you stream?). This is probably a PR that will require more work with writing documentation than actual code, e.g. an extra section at the bottom of this page: https://bunit.egilhansen.com/docs/providing-input/inject-services-into-components.html A paragraph or two that explains how the fallback logic works, and an example that shows how to utilize it with a mocking framework would be great. In other words, as short as possible, but long enough that folks get the point of the feature. |
Good you say that, i'll also add some documentation. |
|
result can still be null, i can't make the return type object? so i was wondering if you would know a better solution? @egil |
In NET5 the |
Thansk for the response, oke thanks, never worked with multiple versions before. But ill do that when i get to my pc. |
@egil I'm currently working on the documentation part. I wanted to add some code samples but I'm not completely sure where to place them, as its not really a component but they are also not testing framework specific. What is your preference? |
@thopdev sounds great. Just a quick response now, more details later if needed:
These are just the ones I could come up with without looking at the code. |
Implemented in #315. |
To enable automatic mocking of services required by components, e.g. when using AutoFixture in combination with a mocking framework, there should be a way to either replace the
TestServicesProvider
or to have it redirect to a user providedIServiceProvider
when it cannot resolve a service itself.This is inspired by the post on using AutoFixture and Moq to simplify the "arrange" step of a test, described here: #307.
The text was updated successfully, but these errors were encountered: