当我阅读scalatra的来源时,我发现有一些代码如下:
protected val _response = new DynamicVariable[HttpServletResponse](null)
protected val _request = new DynamicVariable[HttpServletRequest](null)
Run Code Online (Sandbox Code Playgroud)
有一个有趣的课程名称DynamicVariable
.我看过这堂课的文件,但我不知道何时以及为什么要使用它?它有一个withValue()
通常使用的.
如果我们不使用它,那么我们应该使用什么代码来解决它解决的问题?
(我是scala的新手,如果你能提供一些代码,那就太好了)
我试图模拟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)
Run Code Online (Sandbox Code Playgroud)
在这里阅读,似乎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 …
Run Code Online (Sandbox Code Playgroud)