为什么这个(简单的)单元测试失败了?

发布于 2024-08-22 06:20:44 字数 1439 浏览 9 评论 0原文

这几乎是逐字摘自 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

离笑几人歌 2024-08-29 06:20:44

请尝试以下操作

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 访问写入输出的内容

Try the following instead

class DateTagLibTests extends TagLibUnitTestCase {

    void testThisYear() {
        String expected = Calendar.getInstance().get(Calendar.YEAR)
        tagLib.thisYear()
        assertEquals("years do NOT match", expected, tagLib.out)
    }

}

Your original code has 2 problems:

  • You should not instantiate DateTagLib explicitly. It is already available through a property of the test class named tagLib
  • thisYear does not return the year value, it writes it to out. Within a test you can access the content written to the output via tagLib.out
诗笺 2024-08-29 06:20:44

输出<< Calendar.getInstance().get(Calendar.YEAR) 将结果放入 out,如果您想测试此功能,请使用 def thisYear = { Calendar.getInstance().get (日历.年份) }

out << Calendar.getInstance().get(Calendar.YEAR) puts the result into out, if you want to test this use def thisYear = { Calendar.getInstance().get(Calendar.YEAR) }

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文