Full disclosure: I'm very new to mocking and mocking frameworks. I'm trying to use ScalaMock because it seemed like the 'default' mocking framework to use with ScalaTest but I am happy to use any other framework which is compatible with ScalaTest.
The problem: I've written in Scala a class that talks to a socket. The class has a type parameter of what sort of socket it is to talk to and one of it's arguments is a factory for creating sockets of that type. It has the signature:
class XScanner[T <: SocketClient](
confPath: String = "/etc/default/configPath",
socketClientFactory: String => T
) extends ScannerBase(path)
I would like to be able to write unit tests for this class by supplying a mock SocketClient so my test code doesn't have to connect to a real socket but I can't work out how to do this with ScalaMock.
My test code looks like this:
val t = new XScanner[SocketClient](confPath, (s: String) => mock[SocketClient])
Clearly that won't compile because SocketClient
expects a path to the socket as an argument but I can't call mock[SocketClient(s)]
because that's not a type and I can't call mock[SocketClient](s)
because mock doesn't take the arguments of the type passed to it as it's own arguments.
So how can I write a mock SocketClient
factory to pass to my Scanner? I can't even work out how to mock a class that takes arguments!