簡體   English   中英

Grails-使用reCaptcha插件對控制器的方法進行單元測試

[英]Grails - Unit test a controller's method using the reCaptcha plugin

我想對以下方法進行單元測試:

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為空,或者recaptcha對象包含。 RecaptchaService.groovy的呼叫線路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)

您應該注入recaptchaService並模擬verifyAnswer以返回要為測試用例返回的任何內容。

     def recaptchaServiceMock = mockFor(recaptchaService)

            recaptchaServiceMock.demand.verifyAnswer() {Session session, 
                                                        Map params->
                return true
            }

      currentService.recaptchaService = recaptchaServiceMock.createMock()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM