简体   繁体   English

scala特征中的specs2模拟方法从未验证

[英]specs2 mock methods in a scala trait never verified

I am trying to mock my interactions with an external API that checks a token to see if the user is authorized to perform certain actions (it is currently a seperate API as a PoC that is going to be moved into a middleware later on) 我正在尝试模拟与外部API的交互,该API检查令牌以查看用户是否有权执行某些操作(当前是一个单独的API,作为PoC,稍后将移入中间件)

DEPENDENCIES(SBT DSL) 依赖关系(SBT DSL)

"org.specs2" %% "specs2-core" % "4.2.0" % Test,
"org.specs2" %% "specs2-mock" % "4.2.0" % Test

TEST 测试

class MyHandlerSpec extends Specification with Mockito {
   def is = s2"""
     The first step is to check if the lambda is called on behalf of an 
     authenticated user. This is done by verifying that user token provided 
     as the Authorization header is valid by calling the auth API.

     Here, we can ${ConfirmThat().authLambdaWasCalled} by the handler
   """

 case class ConfirmThat() {
   def interactions = mock[Interactions]

  def authLambdaWasCalled = {
    val reqHandler = Request[Input](
       headers = Map[String, String](
          "Authorization" -> "Bearer gagagaga"
       )
      // millions of values that are not directly related
    )
    MyHandler.handler(reqHandler, null)
    there was one(interactions).authenticateUser(Some("gagagaga"))
  }
 }
}

CODE

The code uses a class MyHandler with extends the trait Interactions : 该代码使用MyHandler类扩展了Interactions特性:

 trait Interactions extends MyServiceTrait with AuthServiceTrait {
    def authenticateUser(token: Option[String]): Future[Either[Errors, Boolean]] = {
  Future.successful(Right(true))
 }
}

class MyHandler extends Interactions with Utils {

  override def handler(request: Request[Input], c: Context): Response[Errors, Output] = { 
    //  get the auth token from the headers as an option, 
    //  there is a method in Utils that I unit tested
    //  it been omitted here for clarity

    val authFuture = authenticateUser(bearerToken)
  }  
}

ERROR 错误

When I run the code, I see the following error: 运行代码时,我看到以下错误:

The mock was not called as expected: 
[error]  Wanted but not invoked:
[error]  interactions.authenticateUser(
[error]      Some(gagagaga)
[error]  );
[error]  -> at com.lendi.lambda.MyHandlerSpec$ConfirmThat.$anonfun$authLambdaWasCalled$1(MyHandlerSpec.scala:71)
[error]  Actually, there were zero interactions with this mock. (MyHandlerSpec.scala:71)

How can I ensure that I can either: 我如何确保自己可以:

a) move the code for authentication into the MyHandler and do a partial mock for the MyHandler using Mockito provided by specs2 a)将身份验证代码移到MyHandler中,并使用specs2提供的Mockito对MyHandler进行部分模拟

b) ensure that I mock the Interactions and inject interactions into the code so that it can be mocked correctly by the handler. b)确保我模拟了交互并将交互注入到代码中,以便处理程序可以正确模拟它。

c) use a DI framework (should I use Spring for my lambda) to inject Interactions and mock it using the DI framework. c)使用DI框架(我应该在自己的lambda中使用Spring)注入交互并使用DI框架对其进行模拟。

You need to pass your mock object to MyHandler 您需要将模拟对象传递给MyHandler

class MyHandler(interactions: Interactions) with Utils

and then 接着

val interactions = mock[Interactions]
val myHandler = MyHandler(interactions)

MyHandler.handler(reqHandler, null)

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

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