大佬教程收集整理的这篇文章主要介绍了Grails – Spock不使用Groovy MetaClass更改为服务测试,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
重复尝试使用MetaClass失败:
> String.MetaClass.blarg产生错误没有这样的属性:blarg for class:java.lang.String
> service.MetaClass.getGithubUrlForPath修改服务实例不起作用
> Githubservice.MetaClass.getGithubUrlForPath修改服务类不起作用
>尝试在测试方法的设置中的MetaClass上添加/修改方法,以及何时块,都没有按预期工作
考试:
package grails.woot import grails.test.mixin.TestFor @TestFor(GithubservicE) class GithubserviceSpec extends spock.lang.Specification { def 'MetaClass test'() { when: String.MetaClass.blarg = { -> 'brainf***' } then: 'some String'.blarg == 'brainf***' } def 'can find repositories for the given username'() { given: def username = 'username' def requestPathParts when: 'the service is called to retrieve JSON' service.MetaClass.getGithubUrlForPath = { pathParts -> requestPathParts = pathParts } service.findRepositoriesByUsername(userName) then: 'the correct path parts are used' requestPathParts == ['users',username,'repos'] } }
服务:
package grails.woot import grails.converters.JSON class Githubservice { def apiHost = 'https://api.github.com/' def findRepositoriesByUsername(userName) { try{ JSON.parse(getGithubUrlForPath('users','repos').text) } catch (FileNotFoundException eX) { // user not found } } def getGithubUrlForPath(String ... pathParts) { "${apiHost}${pathParts.join('/')}".toURL() } }
我已经测试了groovy sHell中的String.MetaClass.blarg示例(由grails启动),它按预期方式执行.
def 'MetaClass test'() { given: String.MetaClass.blarg = { -> 'brainf***' } expect: // note blarg is a method on String MetaClass // not a field,invoke the method 'some String'.blarg() == 'brainf***' } def 'can find repositories for the given username'() { given: def username = 'username' def requestPathParts when: 'the service is called to retrieve JSON' service.MetaClass.getGithubUrlForPath = { String... pathParts -> requestPathParts = pathParts [text: 'blah'] // mimicing URL class } service.findRepositoriesByUsername(userName) then: 'the correct path parts are used' requestPathParts == ['users','repos'] }
以上是大佬教程为你收集整理的Grails – Spock不使用Groovy MetaClass更改为服务测试全部内容,希望文章能够帮你解决Grails – Spock不使用Groovy MetaClass更改为服务测试所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。