2011-09-28 2 views
0

단위 테스트 reCAPTCHA를 플러그인을 사용하여 컨트롤러의 방법 :Grails를 - 내가 단위 다음과 같은 방법을 테스트 할 것

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: "[email protected]", 
                  to: " [email protected] , [email protected] ", 
                  cc: "", 
                  bcc:"[email protected]", 
                  trimmedListOfToRecipients: ["[email protected]", "[email protected]"]) 
     emailSharingCommand.validate() 
} 
controller.handleEmailSharing(emailSharingCommand) 

그러나 나는 다음과 같은 오류가 발생합니다 : 그것은 기본적으로 내 org.codehaus.groovy.grails.commons.ConfigurationHolder를 n이라고 말했다 있기 때문에, 이상하다

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) 

ull 또는 포함 된 recaptcha 개체

if (ConfigurationHolder.config.recaptcha) {...} 

또는 (마지막 :))이 내 RecaptchaConfig.groovy 설정 방법 : RecaptchaService.groovy의 전화 라인 (33)은 내가이 문제를 해결하기 위해 관리하지 않는

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 
    } 
    } 

. configHolder를 테스트 파일로 가져 오려고했지만 아무 것도 변경하지 않았습니다. 어떤 도움이라도 대단히 감사합니다.

답변

4

유닛 테스트는 실행중인 프레임 워크에서 실행되지 않기 때문에 ConfigurationHolder를 가지지 않습니다. 당신은 recaptchaService를 주입하고 테스트 케이스에 대한 반환 원하는대로 돌아갑니다 verifyAnswer을 조롱한다

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) 
+0

감사합니다. 지금은 매우 명확합니다. –

1

: 단위 테스트 방법 내부의이 같은 최고의 옵션의 RecaptchaService을 조롱하는 것입니다 테스트합니다.

 def recaptchaServiceMock = mockFor(recaptchaService) 

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

     currentService.recaptchaService = recaptchaServiceMock.createMock() 
+0

고맙습니다. 이제는 나에게 매우 명확합니다. –

관련 문제