【问题标题】:How to write Expectations with ScalaMock?如何用 ScalaMock 编写期望?
【发布时间】:2015-10-28 01:13:59
【问题描述】:

我知道这个问题似乎很明显,但我已经尝试了documentation 上写的所有内容,但我无法在任何类上模拟单个方法。

对于这个测试,我将 scalaMock 3 用于 Scala 2.10 和 ScalaTest 2

DateServiceTest.scala

@RunWith(classOf[JUnitRunner])
class DateServiceTest extends FunSuite with MockFactory{
  val conf = new SparkConf().setAppName("Simple Application").setMaster("local")

  val sc = new SparkContext(conf)

  implicit val sqlc = new SQLContext(sc)

  val m = mock[DateService]
  (m.getDate _).expects().returning(Some(new DateTime)) // Error here
}

DateService.scala

class DateService extends Serializable {

  def getDate(implicit sqlc: SQLContext): Option[DateTime] = {
    Some(new DateTime(loadDateFromDatabase(sqlc)))
  }
}

这对我来说似乎很简单,但期望是抛出这个错误

type mismatch; found : Option[org.joda.time.DateTime] required: ? ⇒ ?

我在这里做错了吗?还有另一种方法来设置方法的期望吗?

【问题讨论】:

    标签: scala unit-testing scalamock


    【解决方案1】:

    您的 getDate 方法需要 SQLContext - 尝试添加 wildcard (*) 或传递特定的 sqlc

    (m.getDate _).expects(*).returning(Some(new DateTime))
    // Or, alternatively
    (m.getDate _).expects(sqlc).returning(Some(new DateTime))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-07-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-26
      • 1970-01-01
      • 2018-08-26
      • 2023-03-25
      相关资源
      最近更新 更多