I am partially mocking a class that has these two methods:
public void EmitTo(string connectionId, ChatMessage message)
{
Clients.Client(connectionId).broadcastMessage(message.User.UserName, message.Message);
}
public virtual void Broadcast(ChatMessage message)
{
Clients.All.broadcastMessage(message.User.UserName, message.Message);
}
In my test [SetUp]
I have these calls:
hub = Substitute.ForPartsOf<ChatHub>(myMockedClient, context, groupManager);
hub.When(x => x.Broadcast(Arg.Any<ChatMessage>())).DoNotCallBase();
hub.When(x => x.EmitTo(Arg.Any<string>(), Arg.Any<ChatMessage>())).DoNotCallBase();
I have no issues with the Broadcast
call on this line or later on when I call the method (they do not do anything as expected) but oddly my third line throws an error:
System.ArgumentException : Argument cannot be null or empty Parameter name: connectionId
I am a bit lost as I did the exact same thing for both methods and get a different behaviour, why does my when method call the EmitTo
?
NSubstitute like most mocking frameworks can only intercept calls to virtual
methods. It is able to stop the call to Broadcast
, because it is virtual. You need to make EmitTo
virtual if you want to stop it being called. It needs to be:
public virtual void EmitTo(string connectionId, ChatMessage message)
{
Clients.Client(connectionId).broadcastMessage(message.User.UserName, message.Message);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With