开发者

Mocking an action with metaClass in Grails

开发者 https://www.devze.com 2023-03-23 06:05 出处:网络
I\'m trying to mock the action overview of a Controller, but it isn\'t working. I tried this ways: MyController.metaClass.overview = { ->

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

精彩评论

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