Sag*_*ael 5 grails unit-testing spock
嗨,我有一个名为ApiController的控制器,它使用一个名为ApiService的服务,如下所示:
def createCategory(){
def jsonObj = request.JSON
jsonObj.each{ key, value ->
params.put(key,value)
}
render apiService.createCategory(params)
}
Run Code Online (Sandbox Code Playgroud)
哪个工作正常.但我似乎无法为它编写测试.
这是我有多远:
@TestFor(ApiController)
@Mock([Category,ApiService])
class CategorySpec extends Specification {
def setup() {
}
def cleanup() {
}
void "test"() {
setup:
def apiService = Mock(ApiService)
when:
request.method = 'POST'
request.requestMethod = 'POST'
params.categoryID = 'test'
controller.createCategory()
then:
println(response)
1==1
}
Run Code Online (Sandbox Code Playgroud)
从这里我得到以下错误:
java.lang.NullPointerException: Cannot invoke method createCategory() on null object
Run Code Online (Sandbox Code Playgroud)
这显然是因为它无法看到我的apiService bean.所以我的问题是如何在Spock中做到这一点?
它最有可能与Transactional bug:https://github.com/grails/grails-core/issues/1501
ApiService apiService = new ApiService()
controller.apiService = apiService
apiService.transactionManager = Mock(PlatformTransactionManager) { getTransaction(_) >> Mock(TransactionStatus) }
Run Code Online (Sandbox Code Playgroud)
这是一个临时修复(根据错误报告评论)...这对我有用:)
| 归档时间: |
|
| 查看次数: |
3564 次 |
| 最近记录: |