开发者

Grails mockFor and How Best To Test The Method Is Called with Correct Arguments

I want to test that the controller calls the service method with the correct arguments. What is the best way to do that?

My current plan is to use mockFor and then through the closure check the value passed in. Is there开发者_JAVA百科 a better way to do the test through mockFor or the mocked object similar to what I can do with mockito to perform this same method call argument value test?

class HappyControllerTests extends ControllerUnitTestCase {
       :
    void testSomeValue() {
        def mockControl = mockFor(HappyService)
        def givenSomeItem = null
        mockControl.demand.serviceMethod(1..99) { String someItem -> givenSomeItem = someItem; }
        controller.happyService = mockControl.createMock() 

        controller.someAction()

        mockControl.verify()
        assertEquals("specific value", givenSomeItem)
    }
}

Thanks!


I rarely use mockFor as I find groovy's built in metaClass stuff and as ClassName to be easier to work with and more powerful, I'd do this:

void testSomeValue() {
    def givenSomeItem = null
    controller.happyService = [
        serviceMethod: { String someItem -> givenSomeItem = someItem }
    ] as HappyService

    controller.someAction()
    assertEquals "specific value", givenSomeItem
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜