Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ScalaMock. Mock a class that takes arguments

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!

like image 966
jangoolie Avatar asked Jan 11 '13 08:01

jangoolie


1 Answers

The insight is that what you need to mock is socketClientFactory. And then set it up to return a mock SocketClient.

Given:

trait SocketClient {
  def frobnicate(): Unit
}

class ScannerBase(path: String)

class XScanner[T <: SocketClient](
  confPath: String = "/etc/default/configPath",
  socketClientFactory: String => T
) extends ScannerBase(confPath) {
  val socket = socketClientFactory("Some Socket Name")
  socket.frobnicate
}

(side note - your default value for confPath can never be used because there's no default value for socketClientFactory).

then this should get you started (this is with Scala 2.9.x and ScalaMock2 - 2.10.x with ScalaMock3 will be slightly different, but not much so):

import org.scalatest.FunSuite
import org.scalamock.scalatest.MockFactory
import org.scalamock.generated.GeneratedMockFactory

class ScannerTest extends FunSuite with MockFactory with GeneratedMockFactory {

  test("scanner") {
    val mockFactory = mockFunction[String, SocketClient]
    val mockClient = mock[SocketClient]
    mockFactory.expects("Some Socket Name").returning(mockClient)
    mockClient.expects.frobnicate
    val scanner = new XScanner("path/to/config", mockFactory)
  }
}

For completeness, here's the same test in Scala 2.10.0 and ScalaMock3:

import org.scalatest.FunSuite
import org.scalamock.scalatest.MockFactory

class ScannerTest extends FunSuite with MockFactory {

  test("scanner") {
    val mockFactory = mockFunction[String, SocketClient]
    val mockClient = mock[SocketClient]
    mockFactory.expects("Some Socket Name").returning(mockClient)
    (mockClient.frobnicate _).expects()
    val scanner = new XScanner("path/to/config", mockFactory)
  }
}
like image 60
Paul Butcher Avatar answered Oct 29 '22 00:10

Paul Butcher