Grails集成测试过滤器 [英] Grails Integration Test Filter

查看:141
本文介绍了Grails集成测试过滤器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种方法来测试在集成测试中使用过滤器的控制器?

似乎有一种方法使用@Mock注释来进行单元测试,而不是在一个withFilter闭包上包装控制器调用。

但是我不能在集成测试中测试过滤器,从我的pov应该是非常简单的。 p>

更新



所以这里是我找到的解决方案。我没有使用@Mock批注,而是实例化了FiltersUnitTestMixin类,并使用必要的值填充它。

 public class ControllerTest {

def controller = new Controller()
FiltersUnitTestMixin f = new FiltersUnitTestMixin()

@Before
public void setup(){
f.grailsApplication = grailsApplication
f.applicationContext = grailsApplication.mainContext
f。 mockFilters(ControllerFilters)
}

@Test
public void shouldPassTheTest(){
f.withFilters(action:actionName){
controller。 actionName()
}
}
}


解决方案

我有同样的问题,我发现这个 - > http ://ldaley.com/post/392153102/integration-testing-grails-filters



并修改它以适应我的需要

  import grails.util.GrailsWebUtil 
im port org.junit.After
import org.junit.Before
import org.junit.Test

类SomethingIntegrationTests {

def filterInterceptor
def grailsApplication
def grailsWebRequest

@Before
void setUp(){


$ b @ $
)void tearDown(){

}

@Test
void testFilterRedirects(){

def result = request(home ,index,someParameter:2)
assert!result
assert response.redirectedUrl.endsWith(/ * something * /)
}

def getResponse (){
grailsWebRequest.currentResponse
}

def request(Map params,controllerName,actionName){
grailsWebRequest = GrailsWebUtil.bindMockWebRequest(grailsApplication.mainContext)
grailsWebRequest.params.putAll(params)
grailsWebRequest.controllerName =控制器名称
grailsWebRequest.actio nName = actionName $ b $ filterInterceptor.preHandle(grailsWebRequest.request,grailsWebRequest.response,null)
}
}


Is there a way to test a controller which uses a filter on an integration test?

There seems to be a way using the @Mock annotation for Unit Tests and than wrapping the controller call on a withFilter closure.

But I can't get to test filters on an integration test which from my pov should be very straight forward.

Update

So here is the solution I found. Instead of using the @Mock annotation, I instantiated the FiltersUnitTestMixin class and populated it with the necessary values.

public class ControllerTest {  

    def controller = new Controller()  
        FiltersUnitTestMixin f = new FiltersUnitTestMixin()

    @Before
    public void setup() {
        f.grailsApplication = grailsApplication
        f.applicationContext = grailsApplication.mainContext
        f.mockFilters(ControllerFilters)
    }

    @Test
    public void shouldPassTheTest() {
        f.withFilters(action:"actionName") {
            controller.actionName()
        }
    }
}

解决方案

I had the same issue, I found this -> http://ldaley.com/post/392153102/integration-testing-grails-filters

And modified it to suit my needs ending with this

import grails.util.GrailsWebUtil
import org.junit.After
import org.junit.Before
import org.junit.Test

class SomethingIntegrationTests {

    def filterInterceptor
    def grailsApplication
    def grailsWebRequest

    @Before
    void setUp() {

    }

    @After
    void tearDown() {

    }

    @Test
    void testFilterRedirects() {

        def result = request("home", "index", someParameter: "2")
        assert !result
        assert response.redirectedUrl.endsWith(/* something */)
    }

    def getResponse() {
        grailsWebRequest.currentResponse
    }

    def request(Map params, controllerName, actionName) {
        grailsWebRequest = GrailsWebUtil.bindMockWebRequest(grailsApplication.mainContext)
        grailsWebRequest.params.putAll(params)
        grailsWebRequest.controllerName = controllerName
        grailsWebRequest.actionName = actionName
        filterInterceptor.preHandle(grailsWebRequest.request, grailsWebRequest.response, null)
    }
}

这篇关于Grails集成测试过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆