mardi 28 juin 2016

How to mock methods of domain class in unit test which implements trait grails 3.1.4?

I'm not able to mock getter and setter of Domain Class say 'Blog' which implements trait 'Taggable' and extends 'Content' as well :

class Blog extends Content implements Taggable {

In here (Blog : Domain) I have two methods overridden from 'Taggable':

Taggable setTags( List tags ) {
        println "<<< blog setTags called"
        return
    }

    def getTags() {
        println "<<< blog getTags called"
        return []
    } 

and Taggable methods:

    def getTags() {
            println "<<< Taggable getTags"
            return
            this.id ? getTagLinks(Holders.applicationContext.taggableService, this).tag.name : []
        }

   Taggable setTags( List tags ) {
        println "<<< Taggable setTags"
        return
        // remove invalid tags
        tags =  tags?.findAll { it }
....

  }

I want to mock these methods in my BlogControllerSpec :

@Mock([Blog, Content, Tag])
class BlogControllerSpec extends Specification {


    Blog blogInstance

    def setup() {
        //--Mocking Services, Methods and Variables
/*
        Blog.metaClass.setTags = { List tags ->
            println "<<< mock setTags called"
            return
        }

        Blog.metaClass.getTags = { ->
            println "<<< mock getTags called"
            return []
        }*/

        Map args = getContentParams(1) + [publish: true, publishedDate: new Date()]
        blogInstance = new Blog(args) as Taggable

        blogInstance.metaClass.setTags() { ->
            println "<<< MockFor setTags called"
            return
        }
        /*def mockBlog = Mock(Blog) as Taggable
        mockBlog.metaClass.setTags() { ->
            println "<<< MockFor setTags called"
            return
        }
        mockBlog.save(flush: true)*/

        blogInstance.metaClass.setTags = { List tags ->
            println "inside setTags mock"
            return
        }

help me mocking these. Thanks

Aucun commentaire:

Enregistrer un commentaire