LoginSignup
2
2

More than 5 years have passed since last update.

[Grails]ユニットテスト入門(taglib編)

Posted at

参考

http://grails.jp/doc/latest/guide/testing.html
http://grails.org/doc/latest/guide/testing.html

はじめに

今回はGrailsのバージョンが2.4です。
標準でSpockでテストを書きます。
テストを実行するためには、以下のコマンドを実行します。

test-app もしくは test-app unit:

コレで実行可能なユニットテストが実行されます。
他にもオプションなどありますが、基本的に気にしないほうがいいです。テストが実行されなかったりして、混乱のもとになります。

現在自分がGrailsのユニットテストの書き方を勉強中です。
この記事はまだ完成していません。
Qiitaの下書きが埋まってしまったので、とりあえずの公開となります。
今後、内容の修正、追記を都度行います。

豆知識

ユニットテストなので、Grailsは環境をtestとして、DataSource.groovyとBootstrap.groovyをユニットテストが終わった後に読み込ます。
コレは、ユニットテストを環境(データベース)に依存させないためです。
integrationテストが続いて実行される場合には、その情報が利用されます。

テスト用ファイルの生成方法

create-domain-class Testとかcreate-controller Testコマンド、create-tag-lib Testを実行すると、それ用のユニットテスト用ファイルが自動で生成される。

デフォルトは以下のようなソースになっている。

TestSpec.groovy
package パッケージ名

import grails.test.mixin.TestFor
import spock.lang.Specification

/**
 * See the API for {@link grails.test.mixin.domain.DomainClassUnitTestMixin} for usage instructions
 */
@TestFor(Test)
class TestSpec extends Specification {

    def setup() {
    }

    def cleanup() {
    }

    void "test something"() {
    }
}
TestControllerSpec.groovy
package パッケージ名

import grails.test.mixin.TestFor
import spock.lang.Specification

/**
 * See the API for {@link grails.test.mixin.web.ControllerUnitTestMixin} for usage instructions
 */
@TestFor(TestController)
class TestControllerSpec extends Specification {

    def setup() {
    }

    def cleanup() {
    }

    void "test something"() {
    }
}

前提条件

テスト用のドメインはもう作っておいてね!

create-tag-lib Test

TestTagLib.groovy
package パッケージ名

import grails.test.mixin.TestFor
import spock.lang.Specification

/**
 * See the API for {@link grails.test.mixin.web.GroovyPageUnitTestMixin} for usage instructions
 */
@TestFor(TestTagLib)
class TestTagLibSpec extends Specification {

    def setup() {
    }

    def cleanup() {
    }

    void "test something"() {
    }
}

そうすると以下のユニットテストも自動で生成されます。

TestTagLib.groovy
package パッケージ名

class TestTagLib {
    static namespace = "s"

    def hello = { attrs, body ->
        out << "Hello ${attrs.name ?: 'World'}"
    }

    def bye = { attrs, body ->
        out << "Bye ${attrs.name ?: 'World'}"
    }
}

@TestForアノテーションにてテストしたいTagLibを指定する。
代わりに、@TestMixin(GroovyPageUnitTestMixin)でもOK.
この場合、複数のタグライブラリをテストできるようになるが、tagLibが使えなくなったりする。
その代わりなのか、assertOutputMatchesなどの専用メソッドが利用できるようになる。

TestTagLibTestMixin.groovy
package パッケージ名

import grails.test.mixin.TestMixin
import grails.test.mixin.web.GroovyPageUnitTestMixin
import spock.lang.Specification

@TestMixin(GroovyPageUnitTestMixin)
class TestTagLibTestMixinSpec extends Specification{

    def setup() {
    }

    def cleanup() {
    }

    void "タグライブラリの基本的なテスト方法"() {
        setup:
        mockTagLib(TestTagLib)

        expect:
        applyTemplate('<s:hello />') == 'Hello World'
        applyTemplate('<s:hello name="koji" />') == 'Hello koji'
        applyTemplate('<s:bye author="${author}" />', [author: new Test(name:'koji')]) == 'Bye koji'

        // この形式に変化はなし
    }

    void "タグライブラリの基本的なテスト方法(2)" () {

        setup:
        mockTagLib(TestTagLib)

        expect:
        assertOutputEquals('Hello World', '<s:hello />')
        assertOutputMatches(/.*ji*./, '<s:hello name="koji"/>')
    }

}

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2