开发者

Understanding metaClass in Grails tests

I'm currently learning grails, and working through the guide on testing.

There's an example provided which covers writing a test for this piece of code in a fictional BookController:

def show = {
    [ book : Book.get( params.id ) ]
}

The guide suggests the following approach for mocking out the result of params.id:

void testA() {
    BookController.metaClass.getParams = {-> [id:10] }
}

As this is a change on the static definition of BookController, does this persist between tests, or does the Gra开发者_开发知识库ils magic somehow automatically clean up in the tearDown method?

ie, if I was to write a subsequent test that skipped the setup of metaClass.getParams and that ran after testA, would params.id still return 10?

If so, what's the standard grails practice for cleaning up in test tear-down? It doesn't seem to be covered in the guide that I'm reading.


You're using an ancient version of the docs covering 1.0.x. Testing support is a lot more solid now, so see the updated chapter 9 in http://grails.org/doc/latest/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜