跳转至

Mock dates, timestamps, and UUIDs(模拟日期、时间戳和 UUID(Mock dates, timestamps, and UUIDs))

You can specify the output of non-deterministic functions by utilizing jest.spyOn() to inject a mock to run the test.

UUID functions

You can specify the output of Uuid by injecting a mock. Here is an example:

import { MyFunctions } from ".."

import { Objects, ExampleDataFlight } from "@foundry/ontology-api";
import { verifyOntologyEditFunction } from "@foundry/functions-testing-lib";
import { Uuid } from "@foundry/functions-utils";

describe("example test suite", () => {
    const myFunctions = new MyFunctions();

    test("creates new flight", () => {
        const makeUuid = () => "my-uuid";
        jest.spyOn(Uuid, "random").mockImplementation(() => makeUuid());

        verifyOntologyEditFunction(() => myFunctions.createNewFlight())
            .createsObject({
                objectType: ExampleDataFlight,
                properties: {
                    flightId: makeUuid()
                }
            })
    })
});

This can be used to test the following function:

import { Function, OntologyEditFunction, Edits } from "@foundry/functions-api";
import { Objects, ExampleDataFlight } from "@foundry/ontology-api";
import { Uuid } from "@foundry/functions-utils";

export class MyFunctions {
    @Edits(ExampleDataFlight)
    @OntologyEditFunction()
    public createNewFlight(): void {
        Objects.create().exampleDataFlight(Uuid.random());
    }
}

Advanced UUID functions

There are certain circumstances where you may want full control over the output of the Uuid. This requires you to adjust the code of the function you are testing. For example, the createNewFlight function above is wrapped in a class MyFunctions and you can add a constructor to the class that takes a supplier with a default value. The updated function with the supplier looks like this:

import { Function, OntologyEditFunction, Edits } from "@foundry/functions-api";
import { Objects, ExampleDataFlight } from "@foundry/ontology-api";
import { Uuid } from "@foundry/functions-utils";

export class MyFunctions {
    constructor (private UuidSupplier: () => string = Uuid.random){} // this new constructor in the class takes a supplier

    @Edits(ExampleDataFlight)
    @OntologyEditFunction()
    public createNewFlightWithConstructor(): void {
        Objects.create().exampleDataFlight(this.UuidSupplier());
    }
}

This updated function can be tested with full control of the output (in this case we set the generated Uuid to be my-other-uuid):

import { MyFunctions } from ".."

import { Objects , ExampleDataFlight } from "@foundry/ontology-api";
import { verifyOntologyEditFunction } from "@foundry/functions-testing-lib";
import { Uuid } from "@foundry/functions-utils";

describe("example test suite", () => {
    const myFunctions = new MyFunctions();

    test("creates new flight with supplier", () => {
        const myNewFunctions = new MyFunctions(() => "my-other-uuid");

        verifyOntologyEditFunction(() => myNewFunctions.createNewFlightWithConstructor())
            .createsObject({
                objectType: ExampleDataFlight,
                properties: {
                    flightId: "my-other-uuid"
                }
            })

    })
});

Timestamp.now() functions

You can specify the output of Timestamp.now() by injecting a mock. Here is an example:

import { MyFunctions } from ".."

import { Objects , ExampleDataFlight } from "@foundry/ontology-api";
import { verifyOntologyEditFunction } from "@foundry/functions-testing-lib";
import { Timestamp } from "@foundry/functions-api";

describe("example test suite", () => {
    const myFunctions = new MyFunctions();

    test("test timestamp now", () => {
        const makeTimestamp = () => Timestamp.fromISOString("2018-06-13T12:11:13+05:00");
        jest.spyOn(Timestamp, "now").mockImplementation(() => makeTimestamp());

        const flight = Objects.create().exampleDataFlight("flightAnotherTest");
        verifyOntologyEditFunction(() => myFunctions.startTakeoff(flight))
            .modifiesObject({
                object: flight,
                properties: {
                    takeoff: makeTimestamp()
                }
            })
    })
});

This can be used to test the following function:

import { Function, OntologyEditFunction, Edits, Timestamp } from "@foundry/functions-api";
import { Objects, ExampleDataFlight } from "@foundry/ontology-api";

export class MyFunctions {
    @Edits(ExampleDataFlight)
    @OntologyEditFunction()
    public startTakeoff(flight: ExampleDataFlight): void {
        flight.takeoff = Timestamp.now();
    }
}

LocalDate.now() functions

You can specify the output of LocalDate.now() by injecting a mock. Here is an example:

import { MyFunctions } from ".."

import { Objects , ExampleDataFlight } from "@foundry/ontology-api";
import { verifyOntologyEditFunction } from "@foundry/functions-testing-lib";
import { LocalDate } from "@foundry/functions-api";

describe("example test suite", () => {
    const myFunctions = new MyFunctions();

    test("test LocalDate now", () => {
        const makeLocalDate = () => LocalDate.fromISOString("2018-06-13");
        jest.spyOn(LocalDate, "now").mockImplementation(() => makeLocalDate());

        const flight = Objects.create().exampleDataFlight("flightTest");
        verifyOntologyEditFunction(() => myFunctions.dateTakeoff(flight))
            .modifiesObject({
                object: flight,
                properties: {
                    date: makeLocalDate()
                }
            })
    })
});

This can be used to test the following function:

import { Function, OntologyEditFunction, Edits, LocalDate } from "@foundry/functions-api";
import { Objects, ExampleDataFlight } from "@foundry/ontology-api";

export class MyFunctions {
    @Edits(ExampleDataFlight)
    @OntologyEditFunction()
    public dateTakeoff(flight: ExampleDataFlight): void {
        flight.date = LocalDate.now();
    }
}

中文翻译

模拟日期、时间戳和 UUID(Mock dates, timestamps, and UUIDs)

您可以通过使用 jest.spyOn() 注入模拟(mock)来指定非确定性函数(non-deterministic functions)的输出,从而运行测试。

UUID 函数(UUID functions)

您可以通过注入模拟来指定 Uuid 的输出。以下是一个示例:

import { MyFunctions } from ".."

import { Objects, ExampleDataFlight } from "@foundry/ontology-api";
import { verifyOntologyEditFunction } from "@foundry/functions-testing-lib";
import { Uuid } from "@foundry/functions-utils";

describe("example test suite", () => {
    const myFunctions = new MyFunctions();

    test("creates new flight", () => {
        const makeUuid = () => "my-uuid";
        jest.spyOn(Uuid, "random").mockImplementation(() => makeUuid());

        verifyOntologyEditFunction(() => myFunctions.createNewFlight())
            .createsObject({
                objectType: ExampleDataFlight,
                properties: {
                    flightId: makeUuid()
                }
            })
    })
});

这可用于测试以下函数:

import { Function, OntologyEditFunction, Edits } from "@foundry/functions-api";
import { Objects, ExampleDataFlight } from "@foundry/ontology-api";
import { Uuid } from "@foundry/functions-utils";

export class MyFunctions {
    @Edits(ExampleDataFlight)
    @OntologyEditFunction()
    public createNewFlight(): void {
        Objects.create().exampleDataFlight(Uuid.random());
    }
}

高级 UUID 函数(Advanced UUID functions)

在某些情况下,您可能希望对 Uuid 的输出拥有完全控制权。这需要您调整正在测试的函数的代码。例如,上述 createNewFlight 函数被封装在 MyFunctions 类中,您可以向该类添加一个构造函数,该构造函数接受一个带有默认值的提供者(supplier)。更新后的带提供者的函数如下所示:

import { Function, OntologyEditFunction, Edits } from "@foundry/functions-api";
import { Objects, ExampleDataFlight } from "@foundry/ontology-api";
import { Uuid } from "@foundry/functions-utils";

export class MyFunctions {
    constructor (private UuidSupplier: () => string = Uuid.random){} // 类中的这个新构造函数接受一个提供者

    @Edits(ExampleDataFlight)
    @OntologyEditFunction()
    public createNewFlightWithConstructor(): void {
        Objects.create().exampleDataFlight(this.UuidSupplier());
    }
}

这个更新后的函数可以在完全控制输出的情况下进行测试(在本例中,我们将生成的 Uuid 设置为 my-other-uuid):

import { MyFunctions } from ".."

import { Objects , ExampleDataFlight } from "@foundry/ontology-api";
import { verifyOntologyEditFunction } from "@foundry/functions-testing-lib";
import { Uuid } from "@foundry/functions-utils";

describe("example test suite", () => {
    const myFunctions = new MyFunctions();

    test("creates new flight with supplier", () => {
        const myNewFunctions = new MyFunctions(() => "my-other-uuid");

        verifyOntologyEditFunction(() => myNewFunctions.createNewFlightWithConstructor())
            .createsObject({
                objectType: ExampleDataFlight,
                properties: {
                    flightId: "my-other-uuid"
                }
            })

    })
});

Timestamp.now() 函数(Timestamp.now() functions)

您可以通过注入模拟来指定 Timestamp.now() 的输出。以下是一个示例:

import { MyFunctions } from ".."

import { Objects , ExampleDataFlight } from "@foundry/ontology-api";
import { verifyOntologyEditFunction } from "@foundry/functions-testing-lib";
import { Timestamp } from "@foundry/functions-api";

describe("example test suite", () => {
    const myFunctions = new MyFunctions();

    test("test timestamp now", () => {
        const makeTimestamp = () => Timestamp.fromISOString("2018-06-13T12:11:13+05:00");
        jest.spyOn(Timestamp, "now").mockImplementation(() => makeTimestamp());

        const flight = Objects.create().exampleDataFlight("flightAnotherTest");
        verifyOntologyEditFunction(() => myFunctions.startTakeoff(flight))
            .modifiesObject({
                object: flight,
                properties: {
                    takeoff: makeTimestamp()
                }
            })
    })
});

这可用于测试以下函数:

import { Function, OntologyEditFunction, Edits, Timestamp } from "@foundry/functions-api";
import { Objects, ExampleDataFlight } from "@foundry/ontology-api";

export class MyFunctions {
    @Edits(ExampleDataFlight)
    @OntologyEditFunction()
    public startTakeoff(flight: ExampleDataFlight): void {
        flight.takeoff = Timestamp.now();
    }
}

LocalDate.now() 函数(LocalDate.now() functions)

您可以通过注入模拟来指定 LocalDate.now() 的输出。以下是一个示例:

import { MyFunctions } from ".."

import { Objects , ExampleDataFlight } from "@foundry/ontology-api";
import { verifyOntologyEditFunction } from "@foundry/functions-testing-lib";
import { LocalDate } from "@foundry/functions-api";

describe("example test suite", () => {
    const myFunctions = new MyFunctions();

    test("test LocalDate now", () => {
        const makeLocalDate = () => LocalDate.fromISOString("2018-06-13");
        jest.spyOn(LocalDate, "now").mockImplementation(() => makeLocalDate());

        const flight = Objects.create().exampleDataFlight("flightTest");
        verifyOntologyEditFunction(() => myFunctions.dateTakeoff(flight))
            .modifiesObject({
                object: flight,
                properties: {
                    date: makeLocalDate()
                }
            })
    })
});

这可用于测试以下函数:

import { Function, OntologyEditFunction, Edits, LocalDate } from "@foundry/functions-api";
import { Objects, ExampleDataFlight } from "@foundry/ontology-api";

export class MyFunctions {
    @Edits(ExampleDataFlight)
    @OntologyEditFunction()
    public dateTakeoff(flight: ExampleDataFlight): void {
        flight.date = LocalDate.now();
    }
}