testing - grails 4单元测试控制器找不到类
问题描述
我正在使用 grails 4 为控制器创建单元测试 https://testing.grails.org/latest/guide/index.html
我的控制器在 grails-app/controllers/mypack/myfolder/exampleController.groovy 下
我的单元测试在 src/test/groovy/mypack/myfolder/exampleControllerSpec.groovy
我的单元测试就像这个类 exampleControllerSpec extends Specification implements ControllerUnitTest<exampleController>
但它抱怨无法解析符号'exampleController'
这里有什么问题吗?
如何导入示例控制器
解决方案
https://github.com/jeffbrown/leecontrollertest上的项目演示了如何构建测试。
package mypack.myfolder
// NOTE: The nonstandard class naming convention here is intentional
// per https://stackoverflow.com/questions/65723769/grails-4-unit-test-controller-could-not-find-class
class exampleController {
def hello() {
render 'Hello, World!'
}
}
package mypack.myfolder
import grails.testing.web.controllers.ControllerUnitTest
import spock.lang.Specification
// NOTE: The nonstandard class naming convention here is intentional
// per https://stackoverflow.com/questions/65723769/grails-4-unit-test-controller-could-not-find-class
class exampleControllerSpec extends Specification implements ControllerUnitTest<exampleController> {
void "test action which renders text"() {
when:
controller.hello()
then:
status == 200
response.text == 'Hello, World!'
}
}
推荐阅读
- javascript - CallBackUrl 页面上的会话 id 不断变化
- meshlab - 如何测量两个 3D STL 模型之间的豪斯多夫距离
- go - 为什么我在 Go 中遇到分段违规错误
- express - 如何防止所有工作线程执行 cron 作业
- python - 任何人都知道一个简单的方法来编码这个问题?
- php - 有没有办法在 php 中通过引用传递临时数组?
- php - 防止 Laravel Mongo 数据库中的数据覆盖
- google-bigquery - BigQuery 相当于 SELECT * FROM (VALUES ...) AS t
- flutter - Flutter 错误 - 没有为该类型定义方法“setState”
- angular - Angular - 浏览器无法识别新类,将它们视为“未定义”