-
Hello, I am trying to test my project that uses LightInject using MSTests with bUnit (1.1.5). public abstract class BUnitTestContext : TestContextWrapper
{
[TestInitialize]
public void Setup()
{
TestContext = new Bunit.TestContext();
var container = new ServiceContainer(ContainerOptions.Default.WithMicrosoftSettings());
container.RegisterFrom<TestModule>();
container.BeginScope();
var provider = container.CreateServiceProvider(new ServiceCollection());
TestContext.Services.AddFallbackServiceProvider(provider);
}
[TestCleanup]
public void TearDown()
{
TestContext?.Dispose();
}
} The test case looks like this: [TestClass]
public class AddNewOrderUnitTest : BUnitTestContext
{
[TestMethod]
public void Test1()
{
var orderService = TestContext.Services.GetService<DOS_BL.Services.OrderService>(); // this returns the OrderService as it should
var cut = RenderComponent<DOS_PL.Pages.Orders.Add>(); // this throws error
var inputs = cut.FindAll("input");
}
} As you can see, the RenderComponent throws
I tried adding the service using .AddScoped on the TestContext and that worked. But I really don't want to copy all the services manually because I am also using a mapper and it would be a lot to copy. I really don't understand why it throws in the RenderComponent when I can get the service manually in the test. Thanks. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
Hi @dady8889 I do not know how LightInject works, but the fallback service provider is being called when the primary cannot resolve the request, i.e.: public object? GetService(Type serviceType)
=> GetServiceInternal(serviceType);
private object? GetServiceInternal(Type serviceType)
{
if (serviceProvider is null)
serviceProvider = serviceCollection.BuildServiceProvider();
var result = serviceProvider.GetService(serviceType);
if (result is null && fallbackServiceProvider is not null)
result = fallbackServiceProvider.GetService(serviceType);
return result;
} So I am not sure why your code is not working. Please create a minimal reproduction of the issue, but it in a repo or zip file, so I can download and try it locally to figure to figure out what is going on. |
Beta Was this translation helpful? Give feedback.
Hi @dady8889
I do not know how LightInject works, but the fallback service provider is being called when the primary cannot resolve the request, i.e.:
So I am not sure why your code is not working. Please create a minimal reproduction of the issue, but it in a repo or zip…