테스트에서 시각과 UUID 고정
UUID와 현재 시각에 의존하는 함수는 실행마다 값이 달라집니다. TSv1 단위 테스트에서는 jest.spyOn으로 해당 메서드의 응답을 고정하여 기대한 편집을 검사할 수 있습니다.
테스트 대상
가상의 ExampleDataFlight는 String 기본 키 flightId, Timestamp takeoff와 Date date를 가집니다. src/flightClockFunctions.ts에 작성합니다.
import { Edits, LocalDate, OntologyEditFunction, Timestamp } from "@foundry/functions-api";
import { Objects, ExampleDataFlight } from "@foundry/ontology-api";
import { Uuid } from "@foundry/functions-utils";
export class FlightClockFunctions {
@Edits(ExampleDataFlight)
@OntologyEditFunction()
public createNewFlight(): void {
Objects.create().exampleDataFlight(Uuid.random());
}
@Edits(ExampleDataFlight)
@OntologyEditFunction()
public startTakeoff(flight: ExampleDataFlight): void {
flight.takeoff = Timestamp.now();
}
@Edits(ExampleDataFlight)
@OntologyEditFunction()
public dateTakeoff(flight: ExampleDataFlight): void {
flight.date = LocalDate.now();
}
}
jest.spyOn
src/tests/flightClockFunctions.ts의 예제입니다. afterEach에서 spy를 복원하여 고정 응답이 다른 테스트에 남지 않게 합니다.
import { LocalDate, Timestamp } from "@foundry/functions-api";
import { ExampleDataFlight, Objects } from "@foundry/ontology-api";
import { verifyOntologyEditFunction } from "@foundry/functions-testing-lib";
import { Uuid } from "@foundry/functions-utils";
import { FlightClockFunctions } from "../flightClockFunctions";
const functions = new FlightClockFunctions();
afterEach(() => jest.restoreAllMocks());
test("uses the supplied UUID response", () => {
jest.spyOn(Uuid, "random").mockReturnValue("my-uuid");
verifyOntologyEditFunction(() => functions.createNewFlight())
.createsObject({ objectType: ExampleDataFlight, properties: { flightId: "my-uuid" } })
.hasNoMoreEdits();
});
test("uses the timestamp response", () => {
const fixed = Timestamp.fromISOString("2018-06-13T12:11:13+05:00");
jest.spyOn(Timestamp, "now").mockReturnValue(fixed);
const flight = Objects.create().exampleDataFlight("flightAnotherTest");
verifyOntologyEditFunction(() => functions.startTakeoff(flight))
.modifiesObject({ object: flight, properties: { takeoff: fixed } })
.hasNoMoreEdits();
});
test("uses the date response", () => {
const fixed = LocalDate.fromISOString("2018-06-13");
jest.spyOn(LocalDate, "now").mockReturnValue(fixed);
const flight = Objects.create().exampleDataFlight("flightTest");
verifyOntologyEditFunction(() => functions.dateTakeoff(flight))
.modifiesObject({ object: flight, properties: { date: fixed } })
.hasNoMoreEdits();
});
my-uuid는 테스트에서 주입하는 문자열이며 UUID 생성기가 실제로 만든 값이라고 해석하지 않습니다. Timestamp 예제는 +05:00 오프셋이 있는 시각이고 LocalDate 예제는 시각이나 시간대가 없는 날짜입니다.
UUID 공급자 주입
호출마다 사용할 ID를 더 세밀하게 제어하려면 생성자에 공급 함수를 받을 수 있습니다. 기본값을 실제 Uuid.random으로 두면 일반 실행에서는 UUID를 생성합니다.
src/suppliedFlightFunctions.ts입니다.
import { Edits, OntologyEditFunction } from "@foundry/functions-api";
import { Objects, ExampleDataFlight } from "@foundry/ontology-api";
import { Uuid } from "@foundry/functions-utils";
export class SuppliedFlightFunctions {
constructor(private uuidSupplier: () => string = Uuid.random) {}
@Edits(ExampleDataFlight)
@OntologyEditFunction()
public createNewFlightWithConstructor(): void {
Objects.create().exampleDataFlight(this.uuidSupplier());
}
}
src/tests/suppliedFlightFunctions.ts에서는 테스트 공급자만 전달합니다.
import { ExampleDataFlight } from "@foundry/ontology-api";
import { verifyOntologyEditFunction } from "@foundry/functions-testing-lib";
import { SuppliedFlightFunctions } from "../suppliedFlightFunctions";
test("creates a flight with the injected supplier", () => {
const functions = new SuppliedFlightFunctions(() => "my-other-uuid");
verifyOntologyEditFunction(() => functions.createNewFlightWithConstructor())
.createsObject({ objectType: ExampleDataFlight, properties: { flightId: "my-other-uuid" } })
.hasNoMoreEdits();
});
사용할 클래스를 index.ts에서 내보냅니다. TSv2의 UUID는 실제 사용하는 Node 또는 npm 생성기를 기준으로 테스트에서 대체해야 하며 TSv1 Uuid 도구를 그대로 가져오지 않습니다.