I have TagLib, Service and TestCase as follows
How to mock a service in taglib to get the expected result from a service
TagLib:
class SampleTagLib { static namespace = "sample" def baseService def writeName = { attrs, body -> def result = baseService.findAttributeValue(attrs.something) if(result) out << body() } }
Services:
class BaseService { def findAttributeValue(arg1) { return false } }
TagLibUnitTestCase:
import spock.lang.* import grails.plugin.spock.* import org.junit.* import grails.test.mixin.* import grails.test.mixin.support.* import grails.test.mixin.Mock @TestFor(SampleTagLib) @Mock(BaseService) class SampleTagLibSpec extends Specification { def template def setup(){ tagLib.baseService = Mock(BaseService) } def 'writeName'(){ given: tagLib.baseService.findAttributeValue(_) >> true def template ='<sample:writeName something='value'>output</sample:writeName>' when: 'we render the template' def output = applyTemplate(template, [sonething:'value') then: 'output' output =="output" } }
But the condition for receiving the error is not fulfilled. Getting output = ""
Expected output = "output"
source share