为什么这个(简单的)单元测试失败了?
这几乎是逐字摘自 IBM 的 掌握 Grails 系列.
DateTagLib.groovy:
class DateTagLib {
def thisYear = {
out << Calendar.getInstance().get(Calendar.YEAR)
}
}
DateTagLibTests.groovy:
class DateTagLibTests extends TagLibUnitTestCase {
def dateTagLib
protected void setUp() {
super.setUp()
dateTagLib = new DateTagLib()
}
void testThisYear() {
String expected = Calendar.getInstance().get(Calendar.YEAR)
assertEquals("years do NOT match", expected, dateTagLib.thisYear())
}
protected void tearDown() {
super.tearDown()
}
}
grails test-app DateTagLib
输出:
-------------------------------------------------------
Running 1 unit test...
Running test DateTagLibTests...
testThisYear...FAILED
Tests Completed in 359ms ...
-------------------------------------------------------
Tests passed: 0
Tests failed: 1
-------------------------------------------------------
我尝试匹配类型(int/long/String),但我仍然在用头撞墙。
这个测试也失败了:
void testThisYear() {
long expected = Calendar.getInstance().get(Calendar.YEAR)
assertEquals("years do NOT match", expected, (long) dateTagLib.thisYear())
}
This was taken nearly verbatim from IBM's Mastering Grails series.
DateTagLib.groovy:
class DateTagLib {
def thisYear = {
out << Calendar.getInstance().get(Calendar.YEAR)
}
}
DateTagLibTests.groovy:
class DateTagLibTests extends TagLibUnitTestCase {
def dateTagLib
protected void setUp() {
super.setUp()
dateTagLib = new DateTagLib()
}
void testThisYear() {
String expected = Calendar.getInstance().get(Calendar.YEAR)
assertEquals("years do NOT match", expected, dateTagLib.thisYear())
}
protected void tearDown() {
super.tearDown()
}
}
grails test-app DateTagLib
output:
-------------------------------------------------------
Running 1 unit test...
Running test DateTagLibTests...
testThisYear...FAILED
Tests Completed in 359ms ...
-------------------------------------------------------
Tests passed: 0
Tests failed: 1
-------------------------------------------------------
I tried matching the types (int/long/String), but I'm still banging my head against the wall.
This test also fails:
void testThisYear() {
long expected = Calendar.getInstance().get(Calendar.YEAR)
assertEquals("years do NOT match", expected, (long) dateTagLib.thisYear())
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
请尝试以下操作
您的原始代码有 2 个问题:
DateTagLib
。它已经可以通过名为tagLib
的测试类的属性获得。thisYear
不返回年份值,而是将其写入out
。在测试中,您可以通过tagLib.out
访问写入输出的内容Try the following instead
Your original code has 2 problems:
DateTagLib
explicitly. It is already available through a property of the test class namedtagLib
thisYear
does not return the year value, it writes it toout
. Within a test you can access the content written to the output viatagLib.out
输出<< Calendar.getInstance().get(Calendar.YEAR)
将结果放入out
,如果您想测试此功能,请使用def thisYear = { Calendar.getInstance().get (日历.年份) }
out << Calendar.getInstance().get(Calendar.YEAR)
puts the result intoout
, if you want to test this usedef thisYear = { Calendar.getInstance().get(Calendar.YEAR) }