0Pricing
Node.js Backend Development Bootcamp · Ders

Node.js'te Taklitler ve Test Çiftleri

Gerçek bağımlılıkları taklitler, saplamalar ve gözetleyicilerle değiştirerek test edilen kodu nasıl yalıtacağınızı öğrenin; böylece birim testleriniz hızlı ve belirlenebilir kalır.

Node.js'te Taklitler ve Test Çiftleri, CoddyKit'te ücretsiz bir Node.js Backend Development Bootcamp dersidir. Bu, 4 dersinin 4. dersidir. Aşağıdan dersin tamamını ücretsiz okuyabilir, sonra tarayıcıda yerleşik kod editörü ve 7/24 yapay zeka koçu ile uygulamalı olarak pratik yapabilirsin. Bu, Node.js Backend Development Bootcamp öğrenme yolunun bir parçasıdır ve ilerlemeniz web ve CoddyKit uygulaması arasında senkronize olur. Node.js Backend Development Bootcamp kursu toplamda 4 dersten oluşur.

Bu dersin bazı bölümleri henüz çevrilmemiş olup İngilizce olarak gösterilmektedir.

The Problem with Real Dependencies

A unit test should check one piece of logic in isolation. But real code calls databases, APIs, the clock, and the file system — all slow, flaky, and unpredictable.

Test doubles replace those dependencies with controllable fakes.

Types of Test Doubles

Common terms you will hear:

  • Stub: returns a canned value
  • Spy: records how it was called
  • Mock: a stub + spy with built-in expectations
  • Fake: a lightweight working implementation

Creating a Mock Function

In Jest, jest.fn() creates a mock function. It records every call and lets you assert on them later.

const callback = jest.fn();
callback('hello');
expect(callback).toHaveBeenCalledWith('hello');

Defining Return Values

Tell a mock what to return with mockReturnValue, or for async code use mockResolvedValue to resolve a promise.

const getUser = jest.fn().mockResolvedValue({ id: 1, name: 'Ada' });
const user = await getUser();
expect(user.name).toBe('Ada');

Asserting on Calls

Spies let you verify interactions:

  • toHaveBeenCalled()
  • toHaveBeenCalledTimes(n)
  • toHaveBeenCalledWith(args)
const save = jest.fn();
save({ id: 1 });
expect(save).toHaveBeenCalledTimes(1);

Mocking a Module

To replace an entire imported module, use jest.mock(). Every export becomes an auto-mock you can configure per test.

jest.mock('./mailer');
const mailer = require('./mailer');
mailer.send.mockResolvedValue(true);

Spying on Existing Methods

Sometimes you want to watch a real method without replacing it. jest.spyOn() wraps an existing method so you can assert and optionally override it.

const spy = jest.spyOn(console, 'log');
console.log('hi');
expect(spy).toHaveBeenCalledWith('hi');

Resetting Between Tests

Mocks accumulate state. Reset them between tests so calls from one test do not leak into another.

  • mockClear() clears call data
  • mockReset() also clears implementations
afterEach(() => {
  jest.clearAllMocks();
});

Mocking Time

Tests that depend on the current time are flaky. Jest fake timers let you control the clock so timeouts and dates are deterministic.

jest.useFakeTimers();
const fn = jest.fn();
setTimeout(fn, 1000);
jest.advanceTimersByTime(1000);
expect(fn).toHaveBeenCalled();

Dependency Injection Makes Mocking Easy

Code that receives its dependencies as arguments is far easier to test than code that imports them directly. Design for testability.

function createService(db) {
  return { getUser: id => db.find(id) };
}
const fakeDb = { find: jest.fn().mockReturnValue({ id: 1 }) };
const svc = createService(fakeDb);

Don't Over-Mock

Mocking too much produces tests that only confirm your mocks were called — not that the code works. Mock external boundaries (DB, network), but test real logic directly when you can.

Quick Check

Test your understanding of test doubles.

Recap

You learned to isolate code with test doubles:

  • Create mocks with jest.fn() and configure return values
  • Replace modules with jest.mock()
  • Watch real methods with jest.spyOn()
  • Reset mocks between tests and control time with fake timers
  • Inject dependencies for testability and avoid over-mocking

Good mocking keeps unit tests fast, reliable, and focused.

Sıkça Sorulan Sorular

“Node.js'te Taklitler ve Test Çiftleri” dersi ücretsiz mi?

Evet — “Node.js'te Taklitler ve Test Çiftleri” dersin tüm metni burada web'de ücretsiz olarak okunabilir. Etkileşimli olarak pratik yapmak (yerleşik kod editörü ve 7/24 yapay zeka koçu) ve Node.js Backend Development Bootcamp kursunun geri kalanını açmak için CoddyKit PRO'ya yükselt. Node.js Backend Development Bootcamp kursu toplamda 4 dersten oluşur.

“Node.js'te Taklitler ve Test Çiftleri” dersinde ne öğreneceğim?

Gerçek bağımlılıkları taklitler, saplamalar ve gözetleyicilerle değiştirerek test edilen kodu nasıl yalıtacağınızı öğrenin; böylece birim testleriniz hızlı ve belirlenebilir kalır. Node.js Backend Development Bootcamp ile uygulamalı kodu tarayıcıda doğrudan çalıştırarak pratik yaparsın ve 7/24 yapay zeka koçu dersi çalışırken sorularını yanıtlar.

Node.js Backend Development Bootcamp öğrenmeye başlamak için deneyim gerekli mi?

Önceden deneyim gerekmez. CoddyKit'te Node.js Backend Development Bootcamp, başlangıçtan ileri seviyeye kadar yapılandırıldığı için buradan başlayabilir veya başından başlayıp kendi hızında ilerleme yapabilirsin. Bu, 4 dersinin 4. dersidir.

“Node.js'te Taklitler ve Test Çiftleri” dersi ne kadar sürer?

Çoğu CoddyKit dersi yaklaşık 5–10 dakika sürer. Her biri kısa ve etkileşimli olduğu için sabit ilerleme yaparsın ve web ile uygulama arasında tam olarak bıraktığın yerden devam edebilirsin.

Bu Node.js Backend Development Bootcamp dersinde kod yazıp çalıştırabilir miyim?

Evet. Her Node.js Backend Development Bootcamp dersi yerleşik bir kod editörü içerir, bu sayede tarayıcıda gerçek kod yazıp çalıştırabilir ve anlık yapay zeka geri bildirimi alırsın — yerel kurulum gerekli değildir.

Bu kursun tüm dersleri

  1. Jest ile Birim Testleri
  2. API Uç Noktaları için Entegrasyon Testleri
  3. Etkili Hata Ayıklama Teknikleri
  4. Node.js'te Taklitler ve Test Çiftleri
← Node.js Backend Development Bootcamp Sayfasına Dön