gpt4 book ai didi

Grails 集成测试在测试应用程序中不起作用,但单独启动时运行正常

转载 作者:行者123 更新时间:2023-12-02 01:51:42 25 4
gpt4 key购买 nike

我正在测试一个执行一些 gorm-action 的服务类。如果我单独运行测试作为集成测试它运行没有失败,如果我运行测试应用程序(如果我运行测试应用程序集成无关紧要:)我的测试失败并且我收到错误消息,即使用的域类:
“类 [xx.xx.User] 上的方法在 Grails 应用程序之外使用。如果在使用模拟 API 或 Bootstrap Grails 的测试上下文中运行。”

因为它是一个集成测试,我不想模拟域类,我只是不明白这个错误。我正在使用带有正确 tomcat 和 hibernate 插件的 grails 2.3.5:

@TestFor(EntryService)
//@Mock([User, Task, Entry, Admin])
class EntryServiceSpec extends Specification {

Entry entry1
EntryService entryService
User user
Task task
Date date

def setup() {
entryService = new EntryService()
user = User.findByEmail("test@test.de")
task = Task.findByName("something_inserted")
date = new Date().clearTime()

entry1 = new Entry()
entry1.comment = ""
entry1.effort = 23 as BigDecimal
entry1.user = user
entry1.bookedTask = task
entry1.bookedCosts = 300 as BigDecimal
entry1.entryDay = new Date().clearTime()
entry1.save(flush: true)
}

def cleanup() {
if(entry1 != null && entry1.id != null) {
entry1.delete()
}
}

void "Wished effort that shall be added is exceeding 24-hours day-constraints"() {
expect: "user has 23h erfforts, wants to add 2 more hours, it should exceed"
entryService.isEntryEffortExceedingHoursConstraintsPerDay(user, date, new BigDecimal(2)) == true
}

void "Wished effort that shall be added is not exceeding 24-hours day-constraints"() {
expect: "user has 23h erfforts, wants to add 1 more hours, it should not exceed"
entryService.isEntryEffortExceedingHoursConstraintsPerDay(user, date, new BigDecimal(1)) == false
}

void "null parameter should leed to return false"() {
expect: "user is null, method should return false"
entryService.isEntryEffortExceedingHoursConstraintsPerDay(null, date, new BigDecimal(1)) == false

and: "date is null, method should return false"
entryService.isEntryEffortExceedingHoursConstraintsPerDay(user, null, new BigDecimal(1)) == false

and: "wished-effort is null, method should return false"
entryService.isEntryEffortExceedingHoursConstraintsPerDay(user, date, null) == false
}
}

最佳答案

感谢@dmahapatro 在上面的评论中回答了这个问题

您正在将测试作为单元测试而不是集成测试运行...

将您的类(class)签名更改为:

import grails.test.spock.IntegrationSpec

class EntryServiceSpec extends IntegrationSpec

请注意,我还删除了 @TestFor(EntryService)

关于Grails 集成测试在测试应用程序中不起作用,但单独启动时运行正常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22297899/

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