繁体   English   中英

如何模拟Scala单例对象?

[英]How to mock a Scala singleton object?

我试图模拟Scala单例对象。 特别是,我需要模拟服务组件(测试中的类)中使用的对象play.api.libs.ws.WS 使用Mockito这是不可能的,测试执行失败的方式如下:

[error]    MockitoException: : 
[error] Cannot mock/spy class play.api.libs.ws.WS$
[error] Mockito cannot mock/spy following:
[error]   - final classes
[error]   - anonymous classes
[error]   - primitive types  (GeolocationSpec.scala:18)

在这里阅读,似乎Scalamock允许这样做:

要模拟独立的单例对象,请使用org.scalamock.annotation.mockObject

我的服务组件是这样的:

trait GeolocationService {
  def wsClient = WS
  def getPath(origin: Location, destination: Location): Future[Route]
}

class DefaultGeolocationService extends GeolocationService {

  val serviceProviderEndpoint = Play.current.configuration.getString("api.directions.endpoint")

  override def getPath(origin: Location, destination: Location): Future[Route] = {

    val params = Seq(
      "origin" -> s"${origin.lat},${origin.lon}",
      "destination" -> s"${destination.lat},${destination.lon}"
    );
    val resp = wsClient.url(serviceProviderEndpoint.get).withQueryString(params: _*).get()
    resp.map {
      // omitted code
    }
  }
}

我的build.sbt具有所有这些依赖项:

[...]
"org.scalatest" %% "scalatest" % "2.2.1",
"org.specs2" %% "specs2" % "2.3.13" % "test",
"org.scalamock" %% "scalamock-specs2-support" % "3.0.1" % "test",
"org.scalamock" %% "scalamock-scalatest-support" % "3.0.1" % "test",
"org.scalamock" %% "scalamock" % "3.0.1",
[...]

但我找不到这个: org.scalamock.annotation.mockObject

也许这也可以使用EasyMock和PowerMock完成,但我找不到任何Scala示例代码。

任何的想法?

使用ScalaMock 3模拟单例对象是不可能的 ,但Paul Butcher希望在ScalaMock 4中重新引入此功能(请参阅http://paulbutcher.com/2014/04/15/scalamock-status-report/

不要嘲笑单身人士。 而不是WS,使您的服务组件依赖于隐藏它的瘦外观:

trait GeolocationService {
  def ws: (String, Seq[String]) => Promise[Response] = { (url, params) =>
    wsClient.url(serviceProviderEndpoint.get).withQueryString(params: _*).get()
  }
  def getPath(origin: Location, destination: Location): Future[Route]
}

在你的测试中,只需使用mock重写ws方法,现在很容易创建:

val mockedWs = mock[(String, Seq[String]) => Promise[Response]]
// TODO specify mock's behavior here
val service = new DefaultGeolocationService() {
  override def ws = mockedWs
}

为什么你不使用蛋糕模式?

它有点冗长但它会解决你的嘲弄问题。

trait GeolocationServiceComponent {
   val geolocationService:GeolocationService
   trait GeolocationService {      
      def wsClient
      def getPath(origin:Location, destination: Location): Future[Route]   
   }
}

trait GeolocationServiceComponentImpl {
   val geolocationService = new GeolocationService {
      override def wsClient = WS
   }
}


class DefaultGeolocationService extends GeolocationServiceComponent ...

//Defined into your test class
trait MockGeolocationServiceComponent {
       val geolocationService = Mock[GeolocationService]
//Here you define your mock logic       
}

您也可以使用monads来执行此操作,但我从未实现过,这里描述: Scala依赖注入:隐式参数的替代方法

我不喜欢改变设计以适应这种限制。 基本上我能得到的就是改变设计,然后使用模拟框架

  //since mocking frameworks on SCALA is not support mocking singleton
  //hacks are either required to change the design or ...
  //I'd rather putting the mocking logic here by myself
  var testUser:Option[User] = None;

  def getCurrentUser(request: Request[Object]) = {
    if( testUser != None ){
    testUser;
  }

希望能帮助到你。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM