Skip to content

Instantly share code, notes, and snippets.

@misty320
Last active August 29, 2015 14:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save misty320/688a43b2495fd66e3eb0 to your computer and use it in GitHub Desktop.
Save misty320/688a43b2495fd66e3eb0 to your computer and use it in GitHub Desktop.
ScalaMockのメモ書き

Build.scala

sbtのDependenciesに以下を追加。

"org.scalamock" % "scalamock-scalatest-support_2.10" % "3.1.RC1" % "test"

公式では3.0.1を指定しているが、scalatest-2.xとの相性が悪いようなので3.1.RC1を指定。

参考 : paulbutcher/ScalaMock#59

テストクラスにインポート

import org.scalamock.scalatest._

mockの作り方

メソッドの振る舞いを書き換える場合resetExpectation を呼ぶ必要があるが 、現行ではprivate化されてるっぽい?ので、ラップした withExpectationsを呼びその中で記述する。

//FlatSpecを利用
class HogeTest extends org.scalatest.FlatSpec with MockFactory {
  withExpectations {
    val m = mock[Target]
    (m.TargetFunction _).expects(1).returning(10)
    assert(m.TargetFunction(1) === 10)
  }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment