unit-testing - Grails-使用reCaptcha插件对 Controller 的方法进行单元测试

标签 unit-testing grails configuration controller recaptcha

我想对以下方法进行单元测试:

def handleEmailSharing = { EmailSharingCommand esc ->
        if (params.send) {
            def recaptchaOK = true
            if (!recaptchaService.verifyAnswer(session, request.getRemoteAddr(), params)) {
                recaptchaOK = false
            }

         }
...

这就是我试图做到的方式:
import com.megatome.grails.RecaptchaService

class EmailSharerControllerTests extends ControllerUnitTestCase {

    protected void setUp() {
     controller.recaptchaService = new RecaptchaService()
   }

void testHandleEmailSharingSendAndSuccess() {
       mockCommandObject(EmailSharingCommand)
        def emailSharingCommand = new EmailSharingCommand(from: "acorrect@emailaddress.fr", 
                                                          to: "   anothercorrect@emailaddress.fr   ,    whichis@notalone.it   ", 
                                                          cc: "", 
                                                          bcc:"someonein@bcc.com.br",
                                                          trimmedListOfToRecipients: ["anothercorrect@emailaddress.fr", "whichis@notalone.it"])
        emailSharingCommand.validate()
}
controller.handleEmailSharing(emailSharingCommand)

但是我收到以下错误:
Cannot get property 'recaptcha' on null object
java.lang.NullPointerException: Cannot get property 'recaptcha' on null object
    at com.megatome.grails.RecaptchaService.getRecaptchaConfig(RecaptchaService.groovy:33)
    at com.megatome.grails.RecaptchaService.this$2$getRecaptchaConfig(RecaptchaService.groovy)
    at com.megatome.grails.RecaptchaService$this$2$getRecaptchaConfig.callCurrent(Unknown Source)
    at com.megatome.grails.RecaptchaService.isEnabled(RecaptchaService.groovy:100)
    at com.megatome.grails.RecaptchaService$isEnabled.callCurrent(Unknown Source)
    at com.megatome.grails.RecaptchaService.verifyAnswer(RecaptchaService.groovy:81)
    at com.megatome.grails.RecaptchaService$verifyAnswer.call(Unknown Source)
    at bankemist.personalcreditcomparator.EmailSharerController$_closure2.doCall(EmailSharerController.groovy:49)
    at bankemist.personalcreditcomparator.EmailSharerControllerTests.testHandleEmailSharingSendButtonButMissingFromAndTo(EmailSharerControllerTests.groovy:49)

这很奇怪,因为它基本上说我的org.codehaus.groovy.grails.commons.ConfigurationHolder为null或它包含的recaptcha对象。 RecaptchaService.groovy的 call 线路33为:
if (ConfigurationHolder.config.recaptcha) {...}

或(最后一个:))这是我的RecaptchaConfig.groovy的设置方式:
recaptcha {
    // These keys are generated by the ReCaptcha service
    publicKey = "xxx"
    privateKey = "xxx"

    // Include the noscript tags in the generated captcha
    includeNoScript = true
}

mailhide {
    // Generated by the Mailhide service
    publicKey = ""
    privateKey = ""
}

environments {
  development {
    recaptcha {
      // Set to false to disable the display of captcha
      enabled = true

      // Communicate using HTTPS
      useSecureAPI = false
    }
  }
  test {
    recaptcha {
      // Set to false to disable the display of captcha
      enabled = true

      // Communicate using HTTPS
      useSecureAPI = false
    }
  }

我无法解决此问题。
我试图将configurationHolder导入测试文件,但它不会改变任何内容。任何帮助,不胜感激。

最佳答案

单元测试将没有ConfigurationHolder,因为它们不会在运行的框架内执行。在单元测试中,最好的选择是模拟RecaptchaService,因此在您的测试方法中是这样的:

def recapMock = mockFor(RecaptchaService)
recapMock.demand.verifyAnswer(1..1) { session, remoteAddr, params ->
     return true // Or false if you want it to fail in your test
}
controller.recaptchaService = recapMock.createMock()
// Then run your test
controller.handleEmailSharing(emailSharingCommand)

关于unit-testing - Grails-使用reCaptcha插件对 Controller 的方法进行单元测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7588149/

相关文章:

ios - 在 iOS 中通过 Xcode UI XCTests 测试标签栏角标(Badge)

unit-testing - 如何组织集成测试?

configuration - 如何在 SSIS 包运行的命令行上指定密码

configuration - 如何增加 PHPStorm 9 堆大小?

configuration - 如何在 Test Explorer 的 xUnit 中仅显示方法名称?

unit-testing - 您如何编写涉及数据库的单元测试?

java - 如何在不初始化类的情况下模拟内部成员

grails - 如何获取grails中的对象列表?

Grails 命名为层次结构中的查询

grails - 设置环境变量