开发者

Mocking an action with metaClass in Grails

I'm trying to mock the action overview of a Controller, but it isn't working.

I tried this ways:

MyController.metaClass.overview = { ->
        println "MOCKED OVERVIEW"
        render(view:"overview", model)
    }

MyController.metaClass.demand.overview = { ->
        println "MOCKED OVERVIEW"
        render(view:"overview", model)
    }

MyController.metaClass.static.overview = { ->
        p开发者_开发问答rintln "MOCKED OVERVIEW"
        render(view:"overview", model)
    }

All this didn't work.

But i used .metaClass to mock some services, and it worked.

Why with this action isn't working ?

The println statment is never showed.


Action needs to be there during Grails' bootstrap - in order to generate URLs and all the Spring beans. You can't override it (that easy) after application is initialized.

Anyway, you souldn't need to - actions should be simple. Any testable logic should be testable on its own.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜