Savage
Savage
CC#
Created by Savage on 9/13/2023 in #help
✅ Problems with mocking a ServiceProvider
In another server I moderate, it is considered somewhat disrespectful to leave while receiving support.
70 replies
CC#
Created by Savage on 9/13/2023 in #help
✅ Problems with mocking a ServiceProvider
Oh no, I know how to send code 😄 I meant "I am doing something else now, might come back to this discussion later if it is ok"
70 replies
CC#
Created by Savage on 9/13/2023 in #help
✅ Problems with mocking a ServiceProvider
Some other things clicked for me and I am working on those now. Is it ok if I leave this here for now?
70 replies
CC#
Created by Savage on 9/13/2023 in #help
✅ Problems with mocking a ServiceProvider
Also, I have to apologize. I was told this before and that is what I should've done. My bad for not paying attention.
70 replies
CC#
Created by Savage on 9/13/2023 in #help
✅ Problems with mocking a ServiceProvider
Hmm. I still ran into a problem: the ActorProvider constructor takes an IServiceProvider instance as an argument. Thus, to create a substitute for it, I first need to have serviceProvider initialized. Thanks for the insightful response however and the tips! I realize my problem might be too specific/ niche without (especially without full context). Have a great day! 😄
70 replies
CC#
Created by Savage on 9/13/2023 in #help
✅ Problems with mocking a ServiceProvider
Thanks, I need some time to internalize this as it is pretty different from everything else I have seen until now.
70 replies
CC#
Created by Savage on 9/13/2023 in #help
✅ Problems with mocking a ServiceProvider
I don't want the real thing here, I want the one I manually defined in the test.
70 replies
CC#
Created by Savage on 9/13/2023 in #help
✅ Problems with mocking a ServiceProvider
I realized that can't work as I need
var customActorProvider = ...;
serviceProvider.GetService(typeof(IActorProvider))
.Returns(customActorProvider);
var customActorProvider = ...;
serviceProvider.GetService(typeof(IActorProvider))
.Returns(customActorProvider);
If the serviceProvider is not a substitute, I can't use .Returns() on it.
70 replies
CC#
Created by Savage on 9/13/2023 in #help
✅ Problems with mocking a ServiceProvider
I think this might work, thanks!
70 replies
CC#
Created by Savage on 9/13/2023 in #help
✅ Problems with mocking a ServiceProvider
Yes.
70 replies
CC#
Created by Savage on 9/13/2023 in #help
✅ Problems with mocking a ServiceProvider
Thanks, I found something similar here but didn't know if it was what I needed.
70 replies
CC#
Created by Savage on 9/13/2023 in #help
✅ Problems with mocking a ServiceProvider
If I am not missing something (and I looked for it multiple times in the past few days), in the actual program the service provider is initialized when the builder is created. That is what confused me when it came to creating the service provider myself :d
70 replies
CC#
Created by Savage on 9/13/2023 in #help
✅ Problems with mocking a ServiceProvider
Ook, so then I need to have an WebApplicationBuilder like in the actual program, right?
70 replies
CC#
Created by Savage on 9/13/2023 in #help
✅ Problems with mocking a ServiceProvider
Hmm I read it and what I want to test isn't as broad as integration tests are described there. I can't describe it simply as a unit test either as two classes/ subsystems are involved. However, for the test(s) in question, no database or web server are needed.
70 replies
CC#
Created by Savage on 9/13/2023 in #help
✅ Problems with mocking a ServiceProvider
I am not sure I understand your initial question, but let me present why I need CreateScope(). The project uses the actor model. The actor provider (basically a factory) has these:
using var scope = this.serviceProvider.CreateScope();
using var context = scope.ServiceProvider.GetRequiredService<CustomDbContext>();
using var scope = this.serviceProvider.CreateScope();
using var context = scope.ServiceProvider.GetRequiredService<CustomDbContext>();
Thus, I need some way to mock the scope and context for those functions to work. Indeed, in the actual program (not the test), those work right after the WebApplicationBuilder is created (var builder = WebApplication.CreateBuilder(args);) Are you saying that I should go one level deeper in my test and create a substitute/ mock for the builder?
70 replies