I am creating a class that determines which of number of registered WCF client callbacks to call. When a client registers with the server, it supplies the token it is interested in. The class then stores a mapping of token to IClientCallback interface for that token, in a Dictionary.
The method and it's class under test looks like the following
public class Router
{
private IDictionary<int, IClientCallBack> clients;
public Router()
{
clients = new Dictionary<int, IClientCallBack>();
}
public Router(IDictionary<int, IClientCallBack> clients)
{
this.clients = clients;
}
public bool Register(int token, IClientCallBack client)
{
if (!clients.ContainsKey(token))
{
clients.Add(token, client);
return true;
}
return false;
}
}
How do I test the clients are successfully registered with the Router? I figured I can either assume that if the function returned true, it is successful (but what is to stop the function body being only "return true;"?) or I could inject the clients Dictionary into the constructor of the class and in my test I could check that clients.Count equals 1, as per the following.
[TestMethod]
public void RegisterTest()
{
IDictionary<int, IClientCallBack> clients = new Dictionary<int, IClientCallBack>();
var router = new Router(clients);
var client = new Mock<IClientCallBack>().Object;
var success = router.Register(4, client);
Assert.IsTrue(success);
Assert.AreEqual(1, clients.Count);
Assert.AreEqual(clients[4], client);
}
While the test above seems good, it seems like overkill to use dependency injection to insert the collection in, just so I can test it. However it does make testing a lot easier and more accurate (when testing other methods of the class too).
Is this the recommended way to test this method, or is it overkill?