【发布时间】:2018-03-04 10:50:59
【问题描述】:
我可以通过多种方式模拟要测试的类的函数。但是如何模拟在要测试的方法中创建的对象? 我有这个要测试的课
@Grab('org.codehaus.groovy.modules.http-builder:http-builder:0.7')
import groovyx.net.http.HTTPBuilder
class totest {
def get() {
def http = new HTTPBuilder('http://www.google.com')
def html = http.get( path : '/search', query : [q:'Groovy'] )
return html
}
}
如何模拟 http.get 以便测试 get 函数:
class TestTest extends Specification {
def "dummy test"() {
given:
// mock httpbuilder.get to return "hello"
def to_test = new totest()
expect:
to_test.get() == "hello"
}
}
【问题讨论】:
标签: unit-testing groovy mocking spock