カスタム アーティファクト インスタンスを使用するカスタム TagLib を含むプラグインを作成しました。プラグインがアプリケーションに含まれている場合、taglib は期待どおりに機能します。ただし、統合テストを書くことができません。
カスタム アーティファクト タイプが「Foo」で、アーティファクト ハンドラ クラスがFooArtefactHandler
(簡略化された)FooTagLib
クラスは次のようになります。
class FooTagLib {
static namespace = "bar"
def eachFoo = { attrs, body ->
grailsApplication.fooClasses.each { foo ->
out << body()
}
}
}
関連するFooTagLibTests
クラスは次のようになります。
import grails.test.mixin.*
@TestFor(FooTagLib)
class FooTagLibTests {
void testEachFoo() {
grailsApplication.registerArtefactHandler(new FooArtefactHandler())
// Classes AFoo and BFoo are in the test/integration folder
grailsApplication.addArtefact(FooArtefactHandler.TYPE, AFoo)
grailsApplication.addArtefact(FooArtefactHandler.TYPE, BFoo)
// just to check if artefacts are correctly loaded
assert grailsApplication.fooClasses.length == 2
assert applyTemplate("<bar:eachFoo>baz</bar:eachFoo>") == "bazbaz"
}
}
このテストを実行すると、結果は次のようになります。
| Failure: testeachFoo(com.my.package.FooTagLibTests)
| org.codehaus.groovy.grails.web.taglib.exceptions.GrailsTagException: Error executing tag <bar:eachFoo>: No such property: fooClasses for class: org.codehaus.groovy.grails.commons.DefaultGrailsApplication
taglib 内のgrailsApplication
は、テスト内のものと同じインスタンスではないようです。誰かが私にこれを説明できますか?私はここで何か悪いことをしていますか?