Open lopezbertoni opened 2 weeks ago
You could have a constructor specially for testing calling the ServerlessHub
constructor here. The testing constructor would be like:
public SignalRMessageProcessor(
ServiceHubContext serviceHubContext,
IAuthenticationManager authenticationManager,
ILogger<SignalRMessageProcessor> logger) : base(serviceHubContext)
{
_authenticationManager = authenticationManager;
_logger = logger;
}
You can mock the implementation of ServiceHubContext.NegotiateAsync()
for testing.
@Y-Sindo Thanks a lot for the reply.
I don't think I follow your suggestion, the code you provided is injecting the ServiceHubContext
but we're really inheriting from the ServerlessHub
class.
Is your suggestion to change my production code to include the constructor you posted only for testing purposes?
Thanks again for the feedback!
Is your suggestion to change my production code to include the constructor you posted only for testing purposes?
Yes. This is a common practice that we use a different constructor for testing. You may find many examples in other .NET codes. As the main purpose of IServiceProvider
parameter is to provide a ServiceHubContext
instance to the serverless hub, it nearly has the same effect to just replace it with a mocked ServiceHubContext
in your testing.
What version of .NET does your existing project use?
.NET 6
What version of .NET are you attempting to target?
.NET 8
Description
After the migration to .NET 8, in order to use SignalR with Azure Functions, it's required to inherit from ServerlessHub. This is no longer unit testable or at least I couldn't find a way to do so.
Here's an example code that I want to unit test.
Any guidance on this is appreciated. I tried mocking IServiceProvider with no luck.
Project configuration and dependencies
No response
Link to a repository that reproduces the issue
No response