在Spock单元测试中,我试图测试独立于get
GithubUrlForPath的方法findRepositoriesByUsername的行为,两者都属于同一个服务.
重复尝试使用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'] }