tRPC End-to-End Type Safe APIs · บทเรียน

การจำลองบริบทและการพึ่งพาในการทดสอบ tRPC

แยกโพรซีเยอร์ออกจากฐานข้อมูลและบริการจริงด้วยการจำลองบริบท เซสชัน และการพึ่งพาภายนอกของ tRPC ในการทดสอบ

บทเรียน 4 จาก 413 ขั้นตอน

การจำลองบริบทและการพึ่งพาในการทดสอบ tRPC เป็นบทเรียน tRPC End-to-End Type Safe APIs ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน tRPC End-to-End Type Safe APIs และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส tRPC End-to-End Type Safe APIs มีบทเรียนทั้งหมด 4 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

Why Mock the Context

tRPC procedures receive a context holding the db client, the user session, and other services. Real tests should not hit a real database. Mocking the context keeps tests fast and deterministic.

What Lives in Context

A typical context includes:

  • db — the database client
  • session — the authenticated user
  • headers — request metadata

Your createContext function builds this per request.

Building a Test Context

For tests, write a helper that returns a fake context shaped exactly like the real one.

function createTestContext(overrides = {}) {
  return {
    db: mockDb,
    session: null,
    ...overrides,
  };
}

Mocking the Database

Replace the db with an object whose methods are test doubles. With Vitest you can use vi.fn().

const mockDb = {
  user: { findUnique: vi.fn() },
};

Calling a Procedure Directly

Create a caller from your router with the mock context, then invoke procedures like plain functions.

const caller = appRouter.createCaller(createTestContext());
const result = await caller.user.list();

Simulating an Authenticated User

To test a protected procedure, pass a session in the context overrides.

const caller = appRouter.createCaller(
  createTestContext({ session: { user: { id: 'u1' } } })
);

Stubbing Return Values

Tell the mock what to return for a given call so you can assert on the procedure output.

mockDb.user.findUnique.mockResolvedValue({ id: 'u1', name: 'Ada' });

Asserting the Call

Verify the procedure actually queried the db with the expected arguments.

expect(mockDb.user.findUnique).toHaveBeenCalledWith({ where: { id: 'u1' } });

Testing Error Paths

Make the mock throw to confirm your procedure handles failures and maps them to the right tRPC error code.

mockDb.user.findUnique.mockRejectedValue(new Error('DB down'));
await expect(caller.user.get({ id: 'u1' })).rejects.toThrow();

Mocking External Services

Email senders, payment gateways, and other side-effects should also be mocked. Inject them through the context so tests can supply fakes.

const mailer = { send: vi.fn() };
const caller = appRouter.createCaller(createTestContext({ mailer }));

Best Practices

Keep mocked tests reliable:

  • Mirror the real context shape exactly
  • Reset mocks between tests with vi.clearAllMocks()
  • Test both success and failure paths
  • Assert on inputs, not just outputs

Quick Check

Test your mocking knowledge.

Recap

You learned to isolate tRPC procedures in tests:

  • Build a fake context matching the real shape
  • Mock the db and external services with vi.fn()
  • Use createCaller to call procedures directly
  • Stub return values and assert on inputs

Your unit tests are now fast and dependency-free.

เริ่มต้นได้ฟรี

เรียนรู้ tRPC End-to-End Type Safe APIs ด้วย AI tutor — ฟรี

เขียนและเรียกใช้โค้ดจริงในเบราว์เซอร์ของคุณ รับความช่วยเหลือทันทีจาก AI tutor 24/7 และเรียนรู้ต่อจากที่คุณหยุดบนเว็บหรือในแอป

คอร์ส
10
บทเรียน
40

คำถามที่พบบ่อย

บทเรียน “การจำลองบริบทและการพึ่งพาในการทดสอบ tRPC” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “การจำลองบริบทและการพึ่งพาในการทดสอบ tRPC” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส tRPC End-to-End Type Safe APIs ให้อัปเกรดเป็น CoddyKit PRO คอร์ส tRPC End-to-End Type Safe APIs มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “การจำลองบริบทและการพึ่งพาในการทดสอบ tRPC”

แยกโพรซีเยอร์ออกจากฐานข้อมูลและบริการจริงด้วยการจำลองบริบท เซสชัน และการพึ่งพาภายนอกของ tRPC ในการทดสอบ คุณปฏิบัติ tRPC End-to-End Type Safe APIs ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน tRPC End-to-End Type Safe APIs หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน tRPC End-to-End Type Safe APIs บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน

บทเรียน “การจำลองบริบทและการพึ่งพาในการทดสอบ tRPC” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน tRPC End-to-End Type Safe APIs นี้ได้ไหม

ได้ บทเรียน tRPC End-to-End Type Safe APIs ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. การทดสอบหน่วยกระบวนงาน tRPC
  2. การทดสอบการผสานรวมเราเตอร์ tRPC
  3. กลยุทธ์การทดสอบตั้งแต่ต้นจนจบ
  4. การจำลองบริบทและการพึ่งพาในการทดสอบ tRPC
← กลับไปที่ tRPC End-to-End Type Safe APIs