[英]Why is this (trivial) unit test failing?
这几乎完全来自IBM的Mastering 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())
}
尝试以下方法
class DateTagLibTests extends TagLibUnitTestCase {
void testThisYear() {
String expected = Calendar.getInstance().get(Calendar.YEAR)
tagLib.thisYear()
assertEquals("years do NOT match", expected, tagLib.out)
}
}
您的原始代码有2个问题:
DateTagLib
。 它已经可以通过名为tagLib
的测试类的属性使用。 thisYear
不返回年份值,将其写入它out
。 在测试中,您可以通过tagLib.out
访问写入输出的内容 out << Calendar.getInstance().get(Calendar.YEAR)
将结果放入out
,如果要测试此结果,请使用def thisYear = { Calendar.getInstance().get(Calendar.YEAR) }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.