我正在测试的函数返回None
或Some(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)
,其中expectedAnswer
是SignupEmail
的实例,则测试通过。
为什么ArgumentMatchers.any
在Some
内部不起作用?
这不起作用
"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()))
}
}
答案 0 :(得分:2)
您必须使用Scalatest
匹配器而不是Mockito
匹配器来完成
您正在混合概念,模拟匹配器应在存根模拟方法的参数中使用,如果要声明对测试对象的调用结果,则必须使用测试框架提供的匹配器(Scalatest in我的情况就是您的情况),因此基本上检查this页面上的mustBe
和Options
的文档。
提示:如果要检查option
内的内容的类型,可以使用部分函数匹配器并编写类似
signupEmailOption should matchPattern { case Some(_: SignupEmail) => }
答案 1 :(得分:1)
一些选择。
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特性。