Closed rob-king-volpara closed 2 months ago
Looking through the commits to 1.27, I think it stems from the KeyedServiceProvider inclusion: https://github.com/bUnit-dev/bUnit/pull/1405/files#diff-2d39f5d38f8ec09b9d97543e177f6bc88b04440e13e090d0311b02a67101c970
If I change my code to
_testContext.Services.AddKeyedSingleton<NavigationManager, TestNavigationManager>("TestNavigationManager");
it works again.
Hey @rob-king-volpara
That is unfortunate but there is a workaround for that.
The file you provided is somewhat unrelated - it just enables keyed services that were introduced with net8.0.
Your code might work because your TestNavigationManager
isn't used in that case. Do you have anywhere a FromKeyedServices("TestNavigationManager")
?
The reason is here: https://github.com/bUnit-dev/bUnit/commit/8f99afa6269b16439b33c400ef07d64d3f3a9b38
We moved the implementation of the ITestRenderer
and ITestRenderer
itself outside of the DI container.
Instead what you can do is to inject the TestContextBase
in your TestNavigationManager
(see https://github.com/bUnit-dev/bUnit/blob/main/src/bunit.web/TestDoubles/NavigationManager/FakeNavigationManager.cs#L30):
public class TestNavigationManager : NavigationManager
{
private readonly TestContextBase _testContext;
public TestNavigationManager(TestContextBase testContext)
{
Initialize("https://localhost/", "https://localhost/");
_testContext = testContext;
}
protected override void NavigateToCore(string uri, bool forceLoad)
{
Uri = ToAbsoluteUri(uri).ToString();
_testContext.Renderer.Dispatcher.InvokeAsync(() => NotifyLocationChanged(isInterceptedLink: false));
}
}
Ahhh nice one.... I'll give it a try
Cool..... I found I no longer need our TestNavigationManager since the FakedNavigationManager works ok for our cases.
Given this isn't really a bug per se, it can probably be closed.
I have updated my test project from 1.26.64 to 1.28.9 and now I get an exception when test run complaining "Unable to resolve service for type 'Bunit.Rendering.ITestRenderer'"
EDIT: by incrementally updating the package, it looks like the issue was introduced in 1.27.17
No changes were made to my project apart from updating the package version.
The code that triggers the exception is a TestNavigationManager stub that we use to mock nav manager stuff.
The test...
The test nav manager is wired into the TestContext like so:
Results in this output:
Expected behavior:
The tests should all pass as they did with the previous version.
Version info:
Link to reproduction repo. Change the package version in the TestProject1.csproj to see it break. https://github.com/rob-king-volpara/bunit-issue-1472