The method to call Nsubstitute when there is a DoNotCallBase

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 the following 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 problem calling Broadcast on this line or later when I call the method (they do nothing as expected), but, oddly enough, my third line throws an error:

System.ArgumentException: The argument cannot be empty or empty. Parameter Name: connectionId

I got a little lost since I did the same for both methods and got different behavior, why is my method when the method calls EmitTo ?

+6
source share
1 answer

NSubstitute, like most mocking frameworks, can only intercept calls virtual . He can stop the Broadcast call because he is virtual. You must make EmitTo virtual if you want to stop its call. It should be:

 public virtual void EmitTo(string connectionId, ChatMessage message) { Clients.Client(connectionId).broadcastMessage(message.User.UserName, message.Message); } 
+9
source

Source: https://habr.com/ru/post/987730/


All Articles