groovy - Unable to mock Grails Service method when unit testing Controller - MissingMethodException -
am getting following error message when testing controller - see below code. how can correct this? when invoke service method controller (run-app) , works fine.
exception:
groovy.lang.missingmethodexception: no signature of method: grails.test.grailsmock.isok() applicable argument types: (java.lang.string) values: [h] @ ...vcontrollerspec.test something(vcontrollerspec.groovy:)
class: vcontrollerspec
import grails.test.mixin.testfor import spock.lang.specification @testfor(vcontroller) @mock(vservice) class vcontrollerspec extends specification { void "test something"() { given: def vservicemock = mockfor(vservice) vservicemock.demand.isok { string yeah -> return true } controller.vservice = vservicemock.createmock() when: def iso = vservicemock.isok("h") then: iso == true } } class:vservice
import grails.transaction.transactional @transactional class vservice { def isok = { string yeah -> def iso = false return iso } } thanks, steve
assuming there action in vcontroller as:
def myaction() { vservice.isok('hello') } below test should pass
void 'test service'() { given: def vservicemock = mockfor(formatservice) vservicemock.demand.isok { string yeah -> return true } controller.vservice = vservicemock.createmock() when: def iso = controller.myaction() then: iso == true } there few things optimize here including using method isok instead of closure best practices.
Comments
Post a Comment