I am trying to mock a call to a server and verify that the tested code called the correct method. Code structure is as follows:
public interface IServerAdapter
{
void CallServer(Action<IServerExposingToClient> methodToCall, out bool serverCalled);
object CallServer(Func<IServerExposingToClient, object> methodToCall, out bool serverCalled);
}
public interface IServerExposingToClient
{
Resource GetResource(string id);
void AddResource(Resource resource);
}
The code I'm testing accesses an implementation of IServerAdapter
and calls IServerExposingToClient
methods on the server. The reason for this is that we don't want to have to implement every single method (there are quite a lot of them) on the IServerExposingToClient
in a class. Since this is then invoked on the proxy, it works quite well in our code.
Calling a method on the server is done like this:
_mainViewModel.ServerAdapter.CallServer(m => m.AddResource(resource), out serverCalled);
The problem now is testing and mocking. I need to assert that the method (AddResource
) has been called on the server. The out serverCalled
(problem 1) is to make sure the call has made it to the server so logic flows as it should for the caller.
When I use the following Moq setup, I can assert that some method has been called on the server:
Mock<IServerAdapter> serverAdapterMock = new Mock<IServerAdapter>();
bool serverCalled;
bool someMethodCalled = false;
serverAdapterMock.Setup(c => c.CallServer(It.IsAny<Action<IServerExposingToClient>>(), out serverCalled)).Callback(() => someMethodCalled = true);
// assign serverAdapterMock.Object to some property my code will use
// test code
Assert.IsTrue(someMethodCalled, "Should have called method on server.");
As the test says, I can assert that some method has been called on the server. The problem is that I don't know which method has been called. In some methods, the logic I want to test could take different paths depending on conditions, and for some scenarios, several paths might lead to a server call. Because of this, I need to know which method was called so I can have the correct asserts in my test (problem 2).
The variable serverCalled
needs to be set to match a signature for the method call on the IServerAdapter mock. I would also very much like to be able to set that variable inside some callback or whatever which would let the logic of the code I'm testing flow the way it should (problem 1). Because of the way it works now, serverCalled will not be set by the mock setup.
The main problem is not knowing which method was called against the server. I've tried to use Match to check the name of the delegate method, but no sigar.
serverAdapterMock.Setup(c => c.CallServer(Match.Create<Action<IServerExposingToClient>>( x => IsAddResource(x)), out serverCalled)).Callback(() => someMethodCalled = true);
When IsAddResource
is called, the function does not refer to AddResource, only where it was created and what argument it is called with.
Does anyone know how to check which method was called?
For the other problem with out serverCalled
, you could argue that the void
method could be bool
instead, and that the other method could return null
if we don't have a connection to the server (the last argument would be ambiguous as null could either mean object did not exist, or server was unavailable).
I would very much like suggestions for working out both problems.
Thanks in advance