2012-09-28 4 views
4

이 인터셉터를 테스트 할 수있는 방법이 있습니까? 그것은 내 검사에서 무시되고 있습니다.grails의 통합 테스트에서 beforeInterceptor를 테스트하십시오.

코드 :

class BaseDomainController { 
    def beforeInterceptor = { 
     throw new RuntimeException() 
     if(!isAdmin()){ 
      redirect(controller: 'login', action: 'show') 
      return 
     } 
    } 
} 

class BaseDomainControllerSpec extends IntegrationSpec{ 

    BaseDomainController controller = new BaseDomainController() 

    def 'some test'(){ 
     given: 
      controller.index() 
     expect: 
      thrown(RuntimeException) 
    } 

} 

답변

3

이 스레드 http://grails.1312388.n4.nabble.com/Controller-interceptors-and-unit-tests-td1326852.html 그램에 따르면 별도로 인터셉터를 호출해야 나타냅니다. 우리가 토큰을 확인하기 위해 인터셉터를 사용하고는 모든 행동에 대해 동일한 이후 우리의 경우, 우리는 사용 :

각 단위 테스트가 인터셉터에 대해 서로 다른 인수를 지정하는 경우 것 같아요
@Before 
void setUp() 
{ 
    super.setUp(); 
    controller.params.token = "8bf062eb-ec4e-44ae-8872-23fad8eca2ce" 
    if (!controller.beforeInterceptor()) 
    { 
     fail("beforeInterceptor failed"); 
    }  
} 

, 당신이해야합니다 매번 따로 따로 전화 할 수 있습니다. http://grails.org/plugin/functional-test

1

Grails의 문서 상태 : 인터셉터를 호출하지 않습니다

Grails의이하기를 원하지 않을 경우 당신이 전체 라이프 사이클을 통해 갈 것이다 성배의 기능 테스트 같은 것을 사용하는 것 같아요 또는 서블릿 필터를 사용하여 통합 테스트를 수행 할 수 있습니다. 필요한 경우 기능 테스트를 사용하여 격리에서 인터셉터와 필터를 테스트해야합니다.

이것은 단위 테스트에도 적용되며 컨트롤러 작업은 정의 된 인터셉터의 영향을받지 않습니다.

당신이 가진 것을 감안할 때 :

def afterInterceptor = [action: this.&interceptAfter, only: ['actionWithAfterInterceptor','someOther']] 

    private interceptAfter(model) { model.lastName = "Threepwood" } 

당신이해야 인터셉터 테스트하려면 :

가 가로 채기 확인을

void "After interceptor applied to correct actions"() { 

    expect: 'Interceptor method is the correct one' 
    controller.afterInterceptor.action.method == "interceptAfter" 

    and: 'Interceptor is applied to correct action' 
    that controller.afterInterceptor.only, contains('actionWithAfterInterceptor','someOther') 
} 

이 인터셉터 방법은이 있는지 확인 원하는 행동에 적용 원하는 효과

void "Verify interceptor functionality"() { 

    when: 'After interceptor is applied to the model' 
    def model = [firstName: "Guybrush"] 
    controller.afterInterceptor.action.doCall(model) 

    then: 'Model is modified as expected' 
    model.firstName == "Guybrush" 
    model.lastName == "Threepwood" 
} 
,210

또는 더 인터셉터가없는 경우, 어떤

void "Verify there is no before interceptor"() { 
    expect: 'There is no before interceptor' 
    !controller.hasProperty('beforeInterceptor') 
} 

그 예는 인터셉터 후 테스트를했다하지만 같은도 인터셉터 전에 적용해야이없는 확인합니다.

관련 문제