Create stub objects(创建桩对象 (stub objects))¶
You can create and define your mock objects using Objects.create(), which can be used the same way as if it was a regular function. You can then use these mock objects when writing unit tests. Here is an example:
import { MyFunctions } from ".."
import { Objects, ExampleDataAirport } from "@foundry/ontology-api";
describe("example test suite", () => {
const myFunctions = new MyFunctions();
test("test created objects", () => {
const JFK = Objects.create().exampleDataAirport("JFK Test");
JFK.displayAirportName = "John F. Kennedy International";
expect(myFunctions.getAirportName(JFK)).toEqual("John F. Kennedy International");
});
});
For reference, the above example is using the Jest syntax expect(...).toEqual(...) ↗.
中文翻译¶
创建桩对象 (stub objects)¶
您可以使用 Objects.create() 创建并定义模拟对象 (mock objects),其使用方式与常规函数完全相同。随后可在编写单元测试时使用这些模拟对象。示例如下:
import { MyFunctions } from ".."
import { Objects, ExampleDataAirport } from "@foundry/ontology-api";
describe("示例测试套件", () => {
const myFunctions = new MyFunctions();
test("测试已创建对象", () => {
const JFK = Objects.create().exampleDataAirport("JFK Test");
JFK.displayAirportName = "John F. Kennedy International";
expect(myFunctions.getAirportName(JFK)).toEqual("John F. Kennedy International");
});
});
作为参考,上述示例使用了 Jest 语法 expect(...).toEqual(...) ↗。