testing - grails代码覆盖无法访问方法

标签 testing grails code-coverage

我正在尝试使用 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/

相关文章:

hibernate - Multi-Tenancy Grails插件-grails.plugin.multitenant.core.exception.NoCurrentTenantException

header - EclEmma - 类 header 未覆盖

testing - Google Cloud Pub/Sub - 发布测试消息

testing - 现代符号执行技术

ruby-on-rails - 如何在我的 Rails 引擎测试中使用来自 "dummy"的固定装置?

python - 从覆盖范围中跳过文件

docker - 在谷歌云构建期间将 jest 代码覆盖率结果上传到 codecov

Drupal - 测试学生的系统

spring - 如何修复 "Invalid remember-me token (Series/token) mismatch"错误?

hibernate - Grails(Hibernate)确实在插入前进行选择,但未传递id参数值