注: 以下の翻訳の正確性は検証されていません。AIPを利用して英語版の原文から機械的に翻訳されたものです。
jest.spyOn() を使ってモックを注入してテストを実行することで、非決定論的関数の出力を指定できます。
@foundry/functions-utils の Uuid ユーティリティは TypeScript v1 で利用可能で、以下の例で使用しています。TypeScript v2 では、uuid npm パッケージ ↗などの標準的な UUID ライブラリを使い、テスト内で直接モックします。
モックを注入することで、Uuid の出力を指定できます。以下に例を示します。
Copied!1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22import { 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() } }) }) });
これを使って、以下の関数をテストできます。
Copied!1 2 3 4 5 6 7 8 9 10 11import { 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 の出力を完全に制御したい場合があります。そのためには、テスト対象の関数のコードを調整する必要があります。たとえば、上記の createNewFlight 関数は MyFunctions クラス内に定義されており、このクラスに、デフォルト値を持つサプライヤーを受け取るコンストラクターを追加できます。サプライヤーを使うように更新した関数は、以下のようになります。
Copied!1 2 3 4 5 6 7 8 9 10 11 12 13import { 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 に設定しています)。
Copied!1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22import { 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() の出力を指定できます。以下に例を示します。
Copied!1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23import { 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() } }) }) });
これを使って、以下の関数をテストできます。
Copied!1 2 3 4 5 6 7 8 9 10import { 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() の出力を指定できます。以下に例を示します。
Copied!1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23import { 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() } }) }) });
これを使って、以下の関数をテストできます。
Copied!1 2 3 4 5 6 7 8 9 10import { 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(); } }