我想用单元测试覆盖一段代码
$query = "SELECT * FROM Table WHERE ((degrees(acos((sin(radians($actlat))*sin(radians(lat))) + (cos(radians($actlat))*cos(radians(lat))*cos(radians($actlon-lon))))))*111.13384) < 10";
注意:我无法更改该代码
现在,这是使用通配符的单元测试(省略了所有不相关的内容):
public List<Product> fetchProducts() {
...
String userId = anotherObj.getId()
return caller.call(client -> client.getProducts(userId));
}
问题
我想检查功能def anotherObj = Mock( ... )
def caller = Mock( ... )
...
when:
subject.fetchProducts()
then:
1 * anotherObj.getId() >> USER_ID
and:
1 * caller.call(_) >> mockedApiResponse
是否实际上是通过接收参数并使用适当参数调用该参数的函数
伪代码
call
答案 0 :(得分:0)
只要有人碰到这个;
一种解决方案是定义caller
模拟的行为:当caller
用参数调用时,则
Function
代码:
def anotherObj = Mock( ... )
def caller = Mock( ... )
def client = Mock( ... )
...
when:
def response = subject.fetchProducts()
then:
1 * anotherObj.getId() >> USER_ID
and:
1 * caller.call(_) >> { Function lambda ->
lambda.apply(client)
return apiResponse
}
and:
1 * client.getProducts(USER_ID)
and:
response == ...