Stub object searches and aggregations(存根对象搜索与聚合)¶
When writing unit tests you may want to create canned answers (also called "stubs") for object sets searches or object aggregations to dictate the responses to the calls your code is making when writing unit tests. You need to import { whenObjectSet } from "@foundry/functions-testing-lib" to use stubs.
Testing filters on object sets¶
import { Objects } from "@foundry/ontology-api";
const objectSet = Objects.search().objectType();
expect(myFunctions.filterObjectSet(objectSet))
.toEqual(objectSet.filter(s => s.prop.range().gte(0)))
Testing aggregations on an object property using stubs¶
You can define the response to aggregation calls using stubs.
import { whenObjectSet } from "@foundry/functions-testing-lib"
whenObjectSet(Objects.search().objectType().sum(s => s.property)).thenReturn(55);
This means that whenever Objects.search().objectType().sum(s => s.property)) is run, the result will be 55.
Testing objects using stubs¶
You can also define the response to certain object searches using stubs.
import { whenObjectSet } from "@foundry/functions-testing-lib";
whenObjectSet(Objects.search().objectType().orderBy().takeAsync(10)).thenReturn([employeeObj])
await expect(myFunctions.aggregateSum(objectSet)).resolves.toEqual(65);
This means that whenever this particular objects search aggregation is run, the property sum will resolve to 65.
Testing different object sets using stubs¶
You can mock multiple specific object set searches by overloading the search constructor. You must give each object a rid property.
import { whenObjectSet } from "@foundry/functions-testing-lib";
const objA = Objects.create().objectType('a');
const objB = Objects.create().objectType('b');
objA.rid = 'ridA';
objB.rid = 'ridB';
whenObjectSet(Objects.search().ObjType([objA]).all()).thenReturn([objA]);
whenObjectSet(Objects.search().ObjType([objB, objB]).all()).thenReturn([objA, objB]);
中文翻译¶
存根对象搜索与聚合¶
在编写单元测试时,您可能需要为对象集搜索或对象聚合创建预设答案(也称为"存根(stubs)"),以控制代码在调用时的响应结果。您需要从 "@foundry/functions-testing-lib" 中导入 { whenObjectSet } 来使用存根。
测试对象集上的筛选器¶
import { Objects } from "@foundry/ontology-api";
const objectSet = Objects.search().objectType();
expect(myFunctions.filterObjectSet(objectSet))
.toEqual(objectSet.filter(s => s.prop.range().gte(0)))
使用存根测试对象属性的聚合¶
您可以使用存根来定义聚合调用的响应结果。
import { whenObjectSet } from "@foundry/functions-testing-lib"
whenObjectSet(Objects.search().objectType().sum(s => s.property)).thenReturn(55);
这意味着每当执行 Objects.search().objectType().sum(s => s.property)) 时,结果都将返回 55。
使用存根测试对象¶
您还可以使用存根来定义特定对象搜索的响应结果。
import { whenObjectSet } from "@foundry/functions-testing-lib";
whenObjectSet(Objects.search().objectType().orderBy().takeAsync(10)).thenReturn([employeeObj])
await expect(myFunctions.aggregateSum(objectSet)).resolves.toEqual(65);
这意味着每当执行这个特定的对象搜索聚合时,属性求和结果将解析为 65。
使用存根测试不同的对象集¶
您可以通过重载搜索构造函数来模拟多个特定的对象集搜索。您必须为每个对象赋予一个 rid 属性。
import { whenObjectSet } from "@foundry/functions-testing-lib";
const objA = Objects.create().objectType('a');
const objB = Objects.create().objectType('b');
objA.rid = 'ridA';
objB.rid = 'ridB';
whenObjectSet(Objects.search().ObjType([objA]).all()).thenReturn([objA]);
whenObjectSet(Objects.search().ObjType([objB, objB]).all()).thenReturn([objA, objB]);