I have a controller which has a IHubContext<T>
object injected into the constructor. I would like to unit test adding and removing connectionId
s to and from groups. I am asking how I can do this?
I tried this:
public interface IMyHubContext :IHubContext<MyHub>
{
Task AddToGroupAsync(string connectionId, string groupName, CancellationToken cancellationToken = default);
Task RemoveFromGroupAsync(string connectionId, string groupName, CancellationToken cancellationToken = default);
}
var cs = new CancellationTokenSource();
var ct = cs.Token;
Mock<IMyHubContext> hubMoq = new Mock<IMyHubContext>();
hubMoq.Setup(a => a.RemoveFromGroupAsync("123", $"{groupName}", ct)).Returns(null);
But this generates a compile time error.
The compile time error is generated probably due to passing null
to Returns
. Since, RemoveFromGroupAsync
return type is Task
, I think you need to set up this call as below:
hubMoq.Setup(a => a.RemoveFromGroupAsync("123", $"{groupName}", ct))
.Returns(Task.CompletedTask);