gpt4 book ai didi

grails - Grails单元测试:为什么此语句失败?

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

我过去使用Java开发,现在尝试使用this slightly dated tutorial学习Grails / Groovy。

import grails.test.*

class DateTagLibTests extends TagLibUnitTestCase {

def dateTagLib

protected void setUp() {
super.setUp()
dateTagLib = new DateTagLib()
}

protected void tearDown() {
super.tearDown()
}

void testThisYear() {
String expected = Calendar.getInstance().get(Calendar.YEAR)

// NOTE: This statement fails
assertEquals("the years dont match and I dont know why.", expected, dateTagLib.thisYear())

}
}

DateTagLibTests.groovy
( 注意:此TagLibUnitTestCase的用于Grails 1.2.1,而不是 tutorial中使用的版本)

由于某种原因,以上测试失败并显示:

expected:<2010> but was:<2010>



我尝试用以下替代测试版本替换上面的测试,并且测试通过了:
void testThisYear() {
String expected = Calendar.getInstance().get(Calendar.YEAR)
String actual = dateTagLib.thisYear()

// NOTE: The following two assertions work:
assertEquals("the years don\'t match", expected, actual)
assertTrue("the years don\'t match", expected.equals(actual))
}

这两个版本的测试基本上是同一件事吧?

除非Grails 1.2.1或Groovy中有我不了解的新功能。它们应该是相同的类型,因为这两个值都是 Calendar.getInstance()。get(Calendar.YEAR)返回的值

最佳答案

从dateTagLib.thisYear()返回的对象不能为字符串。

尝试

  assertEquals("the years dont match and I dont know why.", expected, dateTagLib.thisYear().toString())

在您的工作示例中,Groovy正在为您将.thisYear()转换为字符串。

确保打印出dateTagLib.thisYear()。class。

干杯

背风处

关于grails - Grails单元测试:为什么此语句失败?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2542470/

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