gpt4 book ai didi

java - 帮助解决失败的 Grails 测试 - 域方法不起作用?

转载 作者:行者123 更新时间:2023-11-30 05:03:21 25 4
gpt4 key购买 nike

鉴于此服务:

class AchievementsService {

static transactional = true

public void onEvent(String eventName, User user) {
def event = Event.findByName(eventName)
if (!event) {
event = new Event(name: eventName, autoConfigured: true)
event.save()
}
}
}

为什么此测试失败:

class AchievementsServiceTests extends GrailsUnitTestCase {
AchievementsService service
User user
protected void setUp() {
super.setUp()
service = new AchievementsService()
user = new User(username:"marty",password: "password")
mockDomain(User,[user])
}

void testThat_given_eventDoesNotExistWhenCallingOnEvent_that_eventIsCreated()
{
mockDomain Event
service.onEvent "MyEvent", user
assert Event.count() == 1
}
}

失败如下:

Assertion failed: 

assert Event.count() == 1
| |
0 false

我一定是在设置中或者在调用 .save() 方法时遗漏了一个步骤,但我看不到它。

最佳答案

您可能会遇到验证失败,请将此代码添加到您的服务中以打印验证错误:

if (!event.save()) { 
event.errors.each{println it}
}

关于java - 帮助解决失败的 Grails 测试 - 域方法不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5913515/

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