如何将一些与 mockito 中的任何值匹配



我的测试函数返回NoneSome(ObjectOfSignupEmail)。在我的测试用例中,我想匹配返回的值是Some(ArgumentMatchers.any[SignupEmail])但出现错误

Expected :Some(null)
Actual   :Some(SignupEmail(Welcome,Test<mailrobot@test.com>,<a href=https://localhost:9000/test/ws/users/signup/11111111-1111-1111-1111-111111111111>Click here to verify email</a>))

如果我将代码更改为signupEmailOption mustBe Some(expectedAnswer)其中expectedAnswerSignupEmail的实例,则测试通过。

为什么ArgumentMatchers.anySome里面不起作用?

这行不通

"createEmailMessageForUserToken for all correct parameters" should {
    "return Some(email)" in {
      val mailConfig = Map("signupUrl"-> "/test/ws/users/signup/",
        "signupFrom"->"Test<mailrobot@test.com>",
        "signupReply"->"Test<noreply@test.com>",
        "signupSubject"->"Welcome")
      val mailerConfig = Map(
        "host" -> "localhost", // (mandatory). The domain of mail server i.e. the server is responsible for sending/receiving emails for this domain
        "port" -> "9000",
        "tlsRequired" -> "yes"
      )
      val newConfig = Map("mail"->mailConfig,
        "play.mailer"->mailerConfig)
      val newConfiguration = Configuration.from(newConfig)
      val testEnv = new TestEnv(newConfiguration)
      val signupEmailOption:Option[SignupEmail] = testEnv.controller.createEmailMessageForUserToken(testEnv.userToken)
       signupEmailOption mustBe Some(ArgumentMatchers.any(SignupEmail.getClass()))
    }

  }

这行得通

 "createEmailMessageForUserToken for all correct parameters" should {
    "return Some(email)" in {
      val mailConfig = Map("signupUrl"-> "/test/ws/users/signup/",
        "signupFrom"->"Test<mailrobot@test.com>",
        "signupReply"->"Test<noreply@test.com>",
        "signupSubject"->"Welcome")
      val mailerConfig = Map(
        "host" -> "localhost", // (mandatory). The domain of mail server i.e. the server is responsible for sending/receiving emails for this domain
        "port" -> "9000",
        "tlsRequired" -> "yes"
      )
      val newConfig = Map("mail"->mailConfig,
        "play.mailer"->mailerConfig)
      val newConfiguration = Configuration.from(newConfig)
      val testEnv = new TestEnv(newConfiguration)
      val url = "https://" + mailerConfig("host") + ":" + mailerConfig("port") + mailConfig("signupUrl") + testEnv.userToken.tokenId
      val html =s"<a href=${url}>Click here to verify email</a>"
      //println("html is "+html)
      val expectedAnswer = SignupEmail(mailConfig("signupSubject"),mailConfig("signupFrom"),html)
      println("expected answer would be "+expectedAnswer)
      val signupEmailOption:Option[SignupEmail] = testEnv.controller.createEmailMessageForUserToken(testEnv.userToken)
      signupEmailOption mustBe Some(expectedAnswer)
 //          signupEmailOption mustBe Some(ArgumentMatchers.any(SignupEmail.getClass()))
    }

  }

你必须使用Scalatest匹配器而不是Mockito匹配器来完成你想要做的事情

您正在混合概念,mockito 匹配器旨在用于存根模拟方法的参数,如果您想断言对测试对象的调用结果,您必须使用测试框架提供的匹配器(在您的情况下,Scalatest 我可以看到(,所以基本上检查此页面以获取 mustBeOptions 的文档。

提示:如果你想检查option里面的东西的类型,你可以使用部分函数匹配器并编写类似的东西

signupEmailOption should matchPattern { case Some(_: SignupEmail) => }

几个选项。

signupEmailOption should not be None
signupEmailOption should not be empty
signupEmailOption shouldBe defined
signupEmailOption should matchPattern { case Some(_) => }
inside(signupEmailOption) { case Some(_) => }

这些都是等价的。

但是你正在做的 - signupEmailOption shouldBe Some(expectedAnswer)实际上是最好的选择。这是正确的做法。就这样吧。

注意should*断言和must*断言几乎是一回事,它们只是取决于你混合了哪个DSL特征。

相关内容

  • 没有找到相关文章

最新更新