模拟重载方法

时间:2013-05-31 20:23:54

标签: unit-testing scala mocking scala-2.10

我使用scalamock来模拟包含一些重载方法的类,但是我遇到了一些错误。

例如:

val out = mock[PrintStream]

(out.print _).expects("cmd \r\n")

引发以下错误:

[error] [...]/src/test/scala/chili.scala:19: ambiguous reference to overloaded definition,
[error] both method print in class PrintStream of type (x$1: String)Unit
[error] and  method print in class PrintStream of type (x$1: Array[Char])Unit

但如果我尝试使用:

(out.print(_: String)).expects("cmd \r\n")

我也收到错误:

[info] [...]/src/test/scala/chili.scala:19: Unable to resolve overloaded method print
[info]       (out.print(_: String)).expects("cmd \r\n")
[info]                 ^
[error] [...]/src/test/scala/chili.scala:19: value expects is not a member of String => Unit
[error]       (out.print(_: String)).expects("cmd \r\n")

有没有办法在scala中执行此操作?也许使用另一个图书馆?

1 个答案:

答案 0 :(得分:12)

我相信您看到的编译器错误与scalamock无法正确模拟PrintStream类的事实有关。如果查看scalamock scaladocs,您将看到声明:

At present, ScalaMock can only mock traits, Java interfaces, and non-final 
classes that define a default constructor

由于PrintStream类既不是接口,也不是默认构造函数,我的猜测是scalamock无法正确模拟它,你看到的错误是其副作用。如果您更改了代码以使用OutputStream(这是一个接口,从而符合scalamock的限制),您可以像这样进行重载方法模拟:

val mockStream = mock[OutputStream]      
(mockStream.write(_:Int)) expects(1)
(mockStream.write(_:Array[Byte])) expects(Array[Byte](1,2,3))

就个人而言,我更喜欢在Specs2内使用的Mockito,因为它没有这些限制。使用PrintWriter的类的示例,然后使用Mockito模拟该类的测试规范如下:

import java.io.PrintStream
import java.io.File
import org.specs2.mutable.Specification
import org.specs2.mock.Mockito

class MockitoExample extends Specification with Mockito{
  val mockPrinter = mock[PrintStream]
  val myPrinter = new MyPrintingClass{
    override val printer = mockPrinter
  }

  "A request to print and attay of strings" should{
    "call println on the PrintStream for each string supplied" in {
      myPrinter print Array("foo", "bar")
      there was one(mockPrinter).println("foo")
      there was one(mockPrinter).println("bar")
    }
  }
}

class MyPrintingClass{
  val printer = new PrintStream(new File("foo.txt"))

  def print(strings:Array[String]) = strings foreach (printer.println(_))
}

现在这是一个非常简单的例子,只使用没有预测试截断的测试后验证(因为println有一个Unit返回类型),但至少你可以看到Mockito做了不受与scalamock相同的限制。您可以阅读有关将Mockito与Specs2 here一起使用的更多信息。