注: 以下の翻訳の正確性は検証されていません。AIPを利用して英語版の原文から機械的に翻訳されたものです。
非決定的な関数の出力を指定するために、jest.spyOn()
を使用してモックを注入し、テストを実行することができます。
Uuid
の出力をモックで指定することができます。以下に例を示します:
Copied!1// 必要なモジュールをインポートする 2import { MyFunctions } from ".." 3 4import { Objects, ExampleDataFlight } from "@foundry/ontology-api"; 5import { verifyOntologyEditFunction } from "@foundry/functions-testing-lib"; 6import { Uuid } from "@foundry/functions-utils"; 7 8// テストスイートを定義する 9describe("example test suite", () => { 10 // MyFunctionsクラスのインスタンスを作成する 11 const myFunctions = new MyFunctions(); 12 13 // 新しいフライトの作成をテストする 14 test("creates new flight", () => { 15 // UUIDを生成する関数を定義する 16 const makeUuid = () => "my-uuid"; 17 // Uuid.randomをモック化し、makeUuid関数を実装する 18 jest.spyOn(Uuid, "random").mockImplementation(() => makeUuid()); 19 20 // myFunctions.createNewFlight()が新しいオブジェクトを作成することを検証する 21 verifyOntologyEditFunction(() => myFunctions.createNewFlight()) 22 .createsObject({ 23 // 作成されるオブジェクトのタイプとプロパティを定義する 24 objectType: ExampleDataFlight, 25 properties: { 26 flightId: makeUuid() 27 } 28 }) 29 }) 30});
これは、以下の関数をテストするために使用できます:
Copied!1import { Function, OntologyEditFunction, Edits } from "@foundry/functions-api"; 2import { Objects, ExampleDataFlight } from "@foundry/ontology-api"; 3import { Uuid } from "@foundry/functions-utils"; 4 5export class MyFunctions { 6 // ExampleDataFlightを編集 7 @Edits(ExampleDataFlight) 8 // オントロジー編集関数 9 @OntologyEditFunction() 10 // 新しいフライトを作成する関数 11 public createNewFlight(): void { 12 // ExampleDataFlightオブジェクトを作成し、ランダムなUUIDを割り当てる 13 Objects.create().exampleDataFlight(Uuid.random()); 14 } 15}
Uuid
の出力に完全に制御したい場合があります。これには、テストしている関数のコードを調整する必要があります。例えば、上記の createNewFlight
関数は、MyFunctions
というクラスでラップされており、デフォルト値を持つサプライヤを引数に取るコンストラクタをクラスに追加することができます。サプライヤを使用した更新された関数は、以下のようになります。
Copied!1import { Function, OntologyEditFunction, Edits } from "@foundry/functions-api"; 2import { Objects, ExampleDataFlight } from "@foundry/ontology-api"; 3import { Uuid } from "@foundry/functions-utils"; 4 5export class MyFunctions { 6 // この新しいコンストラクタは、クラス内でサプライヤを受け取ります 7 constructor (private UuidSupplier: () => string = Uuid.random){} 8 9 // ExampleDataFlightを編集するためのデコレータ 10 @Edits(ExampleDataFlight) 11 // オントロジー編集用の関数デコレータ 12 @OntologyEditFunction() 13 // 新しいフライトを作成するメソッド 14 public createNewFlightWithConstructor(): void { 15 // UUIDサプライヤを使用して新しいフライトを作成 16 Objects.create().exampleDataFlight(this.UuidSupplier()); 17 } 18}
この更新された関数は、出力を完全に制御してテストすることができます(この場合、生成されたUuid
をmy-other-uuid
に設定します):
Copied!1import { MyFunctions } from ".." 2 3import { Objects , ExampleDataFlight } from "@foundry/ontology-api"; 4import { verifyOntologyEditFunction } from "@foundry/functions-testing-lib"; 5import { Uuid } from "@foundry/functions-utils"; 6 7describe("例のテストスイート", () => { 8 const myFunctions = new MyFunctions(); 9 10 test("サプライヤーとともに新しいフライトを作成する", () => { 11 // my-other-uuid を持つ新しい MyFunctions インスタンスを作成 12 const myNewFunctions = new MyFunctions(() => "my-other-uuid"); 13 14 verifyOntologyEditFunction(() => myNewFunctions.createNewFlightWithConstructor()) 15 // 以下のオブジェクトを作成することを検証 16 .createsObject({ 17 objectType: ExampleDataFlight, // オブジェクトタイプは ExampleDataFlight 18 properties: { 19 flightId: "my-other-uuid" // flightId は my-other-uuid 20 } 21 }) 22 23 }) 24});
Timestamp.now()
の出力をモックを注入することで指定できます。以下に例を示します:
Copied!1import { MyFunctions } from ".." 2 3// オントロジAPIから必要なオブジェクトをインポート 4import { Objects , ExampleDataFlight } from "@foundry/ontology-api"; 5// 関数のテスト用にverifyOntologyEditFunctionをインポート 6import { verifyOntologyEditFunction } from "@foundry/functions-testing-lib"; 7// Timestampオブジェクトをインポート 8import { Timestamp } from "@foundry/functions-api"; 9 10// テストスイートを定義 11describe("example test suite", () => { 12 // MyFunctionsのインスタンスを作成 13 const myFunctions = new MyFunctions(); 14 15 // タイムスタンプのテストケース 16 test("test timestamp now", () => { 17 // タイムスタンプを作成する関数 18 const makeTimestamp = () => Timestamp.fromISOString("2018-06-13T12:11:13+05:00"); 19 // Timestamp.now()の実装をモック化 20 jest.spyOn(Timestamp, "now").mockImplementation(() => makeTimestamp()); 21 22 // フライトオブジェクトの作成 23 const flight = Objects.create().exampleDataFlight("flightAnotherTest"); 24 // 関数のテスト 25 verifyOntologyEditFunction(() => myFunctions.startTakeoff(flight)) 26 // オブジェクトが正しく変更されているか確認 27 .modifiesObject({ 28 object: flight, 29 properties: { 30 takeoff: makeTimestamp() 31 } 32 }) 33 }) 34});
この関数のテストに使用できます:
Copied!1// "@foundry/functions-api"から必要なモジュールをインポートします。 2import { Function, OntologyEditFunction, Edits, Timestamp } from "@foundry/functions-api"; 3// "@foundry/ontology-api"から必要なモジュールをインポートします。 4import { Objects, ExampleDataFlight } from "@foundry/ontology-api"; 5 6// MyFunctionsという名前のクラスをエクスポートします。 7export class MyFunctions { 8 // Editsデコレーターを使用して、ExampleDataFlightオブジェクトに対する編集を示します。 9 @Edits(ExampleDataFlight) 10 // OntologyEditFunctionデコレーターを使用して、この関数がオントロジー編集関数であることを示します。 11 @OntologyEditFunction() 12 // startTakeoffという名前のパブリックメソッドを定義します。このメソッドはExampleDataFlightオブジェクトを引数に取り、何も返しません。 13 public startTakeoff(flight: ExampleDataFlight): void { 14 // フライトの離陸時刻を現在の時刻に設定します。 15 flight.takeoff = Timestamp.now(); 16 } 17}
LocalDate.now()
の出力はモックを注入することで指定できます。以下に例を示します:
Copied!1import { MyFunctions } from ".." 2 3import { Objects , ExampleDataFlight } from "@foundry/ontology-api"; 4import { verifyOntologyEditFunction } from "@foundry/functions-testing-lib"; 5import { LocalDate } from "@foundry/functions-api"; 6 7// 例のテストスイート 8describe("example test suite", () => { 9 const myFunctions = new MyFunctions(); 10 11 // LocalDateの現在時刻のテスト 12 test("test LocalDate now", () => { 13 // 2018-06-13のLocalDateを生成する関数 14 const makeLocalDate = () => LocalDate.fromISOString("2018-06-13"); 15 // LocalDate.now()の挙動を上書き 16 jest.spyOn(LocalDate, "now").mockImplementation(() => makeLocalDate()); 17 18 // フライトオブジェクトの生成 19 const flight = Objects.create().exampleDataFlight("flightTest"); 20 // dateTakeoff関数の検証 21 verifyOntologyEditFunction(() => myFunctions.dateTakeoff(flight)) 22 .modifiesObject({ 23 object: flight, 24 properties: { 25 date: makeLocalDate() 26 } 27 }) 28 }) 29});
次の関数をテストするために使用できます:
Copied!1// "@foundry/functions-api" からいくつかのクラスと関数をインポートします 2import { Function, OntologyEditFunction, Edits, LocalDate } from "@foundry/functions-api"; 3// "@foundry/ontology-api" からもいくつかのクラスと関数をインポートします 4import { Objects, ExampleDataFlight } from "@foundry/ontology-api"; 5 6// MyFunctionsというクラスをエクスポートします 7export class MyFunctions { 8 // ExampleDataFlightを編集することを示すデコレータ 9 @Edits(ExampleDataFlight) 10 // OntologyEditFunctionというデコレータ 11 @OntologyEditFunction() 12 // dateTakeoffというパブリックメソッド。引数としてflight(ExampleDataFlightのインスタンス)をとります 13 public dateTakeoff(flight: ExampleDataFlight): void { 14 // flightのdateプロパティに現在の日付を設定します 15 flight.date = LocalDate.now(); 16 } 17}