我是初学者,我尝试使用MockFor模拟或模拟grails tagLib中的参数?

时间:2015-11-08 00:02:35

标签: unit-testing grails testing

示例TagLib:    我需要嘲笑' params'在某些请求中使用

class MyTagLib {
   static namespace = 'p'

   format = { attrs -->
      def siteId = params.siteId // I need to mock this 
      def domainLanguage = params.domainLanguage.toString()
      .
      .
      . 
   }

1 个答案:

答案 0 :(得分:0)

您应该首先查看http://grails.github.io/grails-doc/latest/guide/testing.html#unitTestingTagLibraries

在你的情况下,你可能会做类似的事情:

@TestFor(MyTagLib)
class MyTagLibSpec extends Specification {
    def "test something with params"() {
       given:
       params.siteId == "mySiteId"
       params.domainLanguage = new DomainLanguage()

       when:
       def result = tagLib.format(attr1: 'someAtribute').toString()

       then:
       result == 'expectedOutput'  
    }
}

但我会首先尝试阅读文档: - )