我正在尝试使用 Coverage 在 grails 2 中进行测试,但我无法访问方法:
它说:
“java.lang.IllegalStateException:未找到线程绑定(bind)请求:您是指实际Web请求之外的请求属性,还是在原始接收线程之外处理请求?如果您实际上是在Web请求中操作并且仍然接收此消息,您的代码可能在 DispatcherServlet/DispatcherPortlet 之外运行:在这种情况下,请使用 RequestContextListener 或 RequestContextFilter 来公开当前请求..."
class UserController implements ResourceLoaderAware{
def index(Integer max) {
params.max = Math.min(max ?: 10, 100)
respond User.list(params), model:[userInstanceCount: User.count()]
}
}
import grails.test.mixin.Mock
import grails.test.mixin.TestMixin
import grails.test.mixin.support.GrailsUnitTestMixin
import spock.lang.Specification
import com.snt.olucarodashboard.UserController
import grails.test.mixin.TestFor
@TestMixin(GrailsUnitTestMixin)
class UserControllerSpec extends Specification {
UserController controller=new UserController()
void "Test the index action returns the correct model"() {
when: "The index action is executed"
controller.index(0)
}
}
问题是什么?
谢谢
最佳答案
与其自己实例化 Controller ,不如使用 @TestFor
类级别的注释
import grails.test.mixin.TestFor
@TestMixin(GrailsUnitTestMixin)
@TestFor( UserController )
class UserControllerSpec extends Specification {
void "Test the index action returns the correct model"() {
when: "The index action is executed"
controller.index(0)
}
}
关于testing - grails代码覆盖无法访问方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52424230/