質問

これは近くのバーベイタムから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/長/文字列)が、まだヘ頭です。

この試験に失敗した:

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帰属
所属していません StackOverflow
scroll top