gpt4 book ai didi

unit-testing - Intellij 13 中的单元测试失败但通过命令行通过

转载 作者:行者123 更新时间:2023-12-04 05:27:57 25 4
gpt4 key购买 nike

我有一个非常简单的单元测试,用于测试将当前用户呈现为 JSON 的 Controller 操作。这是我的测试课:

@TestFor(UserController)
class UserControllerSpec extends Specification {

def setup() {
def springSecurityService = mockFor(SpringSecurityService)
springSecurityService.demand.getCurrentUser { -> [name: "Jim"] }
controller.springSecurityService = springSecurityService.createMock()
}

def cleanup() {
}

void "test current"() {
when:
request.method = 'GET'
controller.current()

then:
response.json.name == "Jim"
}
}

这是 Controller (省略非相关代码)
def springSecurityService

static responseFormats = ['json']

static allowedMethods = [current: "GET"]

def current() {
respond(springSecurityService.currentUser)
}

我正在通过 grails test -unit package.class 单独执行此测试它通过命令行工作:
|Running 1 unit test... 1 of 1
|Completed 1 unit test, 0 failed in 0m 11s
.
|Tests PASSED - view reports in ...\target\test-reports

这不能通过 IntelliJ 工作,因为 springSecurityService.currentUser始终为空。这是由此产生的异常:
org.codehaus.groovy.grails.web.converters.exceptions.ConverterException: Error parsing JSON
at grails.converters.JSON.parse(JSON.java:292)
at org.codehaus.groovy.grails.plugins.testing.AbstractGrailsMockHttpServletResponse.getJson(AbstractGrailsMockHttpServletResponse.groovy:97)
at massemailsystem.UserControllerSpec.test current(UserControllerSpec.groovy:28)
Caused by: org.codehaus.groovy.grails.web.json.JSONException: Missing value. at character 0 of
at org.codehaus.groovy.grails.web.json.JSONTokener.syntaxError(JSONTokener.java:470)
at org.codehaus.groovy.grails.web.json.JSONTokener.nextValue(JSONTokener.java:357)
at grails.converters.JSON.parse(JSON.java:283)
... 2 more

有任何想法吗?

最佳答案

您需要将此测试用例作为 IntelliJ 中的 grails 测试用例运行。 enter image description here enter image description here

关于unit-testing - Intellij 13 中的单元测试失败但通过命令行通过,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23792131/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com