我编写了一个过滤规则,我想用grails集成测试来测试。 过滤器是
invalidAccess(controller: "home") {
before = {
redirect(controller: "newHome", action: "index")
return false
}
}
我已按照此链接编写集成测试 http://ldaley.com/post/392153102/integration-testing-grails-filters
返回结果为false但是给出
null for redirectedUrl
而不是newHome&索引方法网址。 我在这里缺少什么?
import grails.util.GrailsWebUtil
class MyFilterTests extends GroovyTestCase {
def filterInterceptor
def grailsApplication
def grailsWebRequest
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)
}
def getResponse() {
grailsWebRequest.currentResponse
}
def testFilterRedirects() {
def result = request( someParameter: "2", "home", "index")
assertFalse result
assertTrue response.redirectedUrl.endsWith(/* something */)
}
}
答案 0 :(得分:1)
如果您想尝试unit testing
并需要模拟某些服务,那么您可以模拟:
@TestFor(SomethingToTest)
@Mock([FirstService, SecondService])
class SomethingToTestSpec extends Specification {
然后你想要integration test
然后尝试按照测试
import grails.util.GrailsWebUtil
import org.junit.After
import org.junit.Before
import org.junit.Test
class MyFilterIntegrationTests {
def filterInterceptor
def grailsApplication
def grailsWebRequest
@Before
void setUp() {
}
@After
void tearDown() {
}
@Test
void testFilterRedirects() {
def result = request("person", "index", someParameter: "2")
assert !result
assert response.redirectedUrl.endsWith('/auth/index')
}
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)
}
}