Objective-C iOS Development for Legacy & Enterprise Apps · Lekcja

Pisanie testów charakterystyki przed refaktoryzacją

Nie można bezpiecznie refaktoryzować nieprzetestowanego starszego kodu. Ta lekcja uczy testowania charakterystyki: rejestrowania bieżącego zachowania starszego kodu Objective-C, aby refaktoryzacja go nie zmieniła.

Lekcja 4 z 413 kroki

Pisanie testów charakterystyki przed refaktoryzacją to bezpłatna lekcja Objective-C iOS Development for Legacy & Enterprise Apps na CoddyKit. To lekcja 4 z 4. Możesz przeczytać całą lekcję poniżej za darmo — a potem ćwiczyć ją interaktywnie w przeglądarce z wbudowanym edytorem kodu i tutorem AI dostępnym 24/7. To część ścieżki edukacyjnej Objective-C iOS Development for Legacy & Enterprise Apps, a Twój postęp synchronizuje się między webem a aplikacją CoddyKit. Kurs Objective-C iOS Development for Legacy & Enterprise Apps zawiera 4 lekcji w sumie.

Części tej lekcji nie zostały jeszcze przetłumaczone i są wyświetlane po angielsku.

The Refactoring Dilemma

Refactoring should not change behavior, but legacy code often has no tests proving what its behavior even is. Characterization tests solve this.

What Is a Characterization Test

A characterization test documents the code as it actually behaves, not as it should behave. It pins current output so any change becomes visible.

Find a Seam

A seam is a place you can call into the code without running the whole app. Public methods on a class are the easiest seams.

@interface PriceCalculator : NSObject
- (NSDecimalNumber *)totalForItems:(NSArray *)items
                         withTax:(double)taxRate;
@end

Capture Current Output

Call the method with known inputs and record whatever it returns today, even if it looks wrong.

// Run once, observe, then assert the observed value
NSDecimalNumber *result = [calc totalForItems:items withTax:0.1];
NSLog(@"observed: %@", result); // -> 22.00

Pin It With an Assertion

Turn the observed value into an assertion. This test now fails if behavior changes.

- (void)testTotalCharacterization {
    NSDecimalNumber *r = [calc totalForItems:items withTax:0.1];
    XCTAssertEqualObjects(r, [NSDecimalNumber decimalNumberWithString:@"22.00"]);
}

Cover the Edge Cases

Add tests for empty input, zero tax, and large values. The goal is to surround the code so the refactor has a safety net.

Breaking Dependencies

If a method touches the network or disk, introduce a protocol seam and inject a fake so the test is fast and deterministic.

@protocol PriceFeed <NSObject>
- (double)currentRate;
@end
// Inject a fake PriceFeed in the test

Refactor Under Green

With tests green, refactor in small steps, re-running tests after each change. Any red means you changed behavior and must reconsider.

Distinguish Bug From Behavior

A characterization test may pin a bug. That is fine: first preserve it, then fix it as a separate, deliberate change with its own test.

Delete When Replaced

Once proper unit tests describe the desired behavior, you can retire characterization tests that merely pinned legacy quirks.

Build the Net Incrementally

You do not need full coverage at once. Add characterization tests only around the code you are about to touch; the net grows as you refactor.

Quick Check

Test your characterization testing knowledge.

Recap

You learned to find seams, capture and pin current behavior, break dependencies with protocols, refactor under green, and grow a test net incrementally around legacy Objective-C.

Bezpłatny start

Ucz się Objective-C dzięki korepetycjom AI — za darmo

Pisz i uruchamiaj kod w przeglądarce, otrzymuj natychmiastową pomoc od korepetytora AI dostępnego 24/7 i kontynuuj naukę w sieci lub w aplikacji.

Kursy
12
Lekcje
48

Często zadawane pytania

Czy lekcja „Pisanie testów charakterystyki przed refaktoryzacją” jest bezpłatna?

Tak — pełny tekst „Pisanie testów charakterystyki przed refaktoryzacją” jest dostępny za darmo tutaj w sieci. Aby ćwiczyć ją interaktywnie (wbudowany edytor kodu i tutor AI dostępny 24/7) i odblokować resztę kursu Objective-C iOS Development for Legacy & Enterprise Apps, przejdź na CoddyKit PRO. Kurs Objective-C iOS Development for Legacy & Enterprise Apps zawiera 4 lekcji w sumie.

Co nauczysz się w „Pisanie testów charakterystyki przed refaktoryzacją”?

Nie można bezpiecznie refaktoryzować nieprzetestowanego starszego kodu. Ta lekcja uczy testowania charakterystyki: rejestrowania bieżącego zachowania starszego kodu Objective-C, aby refaktoryzacja go… Ćwiczysz Objective-C iOS Development for Legacy & Enterprise Apps z praktycznym kodem, który uruchamiasz bezpośrednio w przeglądarce, a tutor AI dostępny 24/7 odpowiada na Twoje pytania podczas pracy nad lekcją.

Czy potrzebuję doświadczenia, aby zacząć Objective-C iOS Development for Legacy & Enterprise Apps?

Nie wymagamy żadnego doświadczenia. Objective-C iOS Development for Legacy & Enterprise Apps w CoddyKit jest strukturyzowany dla początkujących i zaawansowanych użytkowników, więc możesz zacząć tutaj lub od początku i uczyć się w swoim tempie. To lekcja 4 z 4.

Ile czasu zajmuje lekcja „Pisanie testów charakterystyki przed refaktoryzacją”?

Większość lekcji CoddyKit trwa około 5–10 minut. Każda lekcja to mały, interaktywny krok, dzięki czemu robisz systematyczne postępy i zawsze wracasz dokładnie do tego samego miejsca — na webie i w aplikacji.

Czy mogę pisać i uruchamiać kod w tej lekcji Objective-C iOS Development for Legacy & Enterprise Apps?

Tak. Każda lekcja Objective-C iOS Development for Legacy & Enterprise Apps zawiera wbudowany edytor kodu, więc piszesz i uruchamiasz prawdziwy kod bezpośrednio w przeglądarce i od razu otrzymujesz sprzężenie zwrotne od AI — bez konfiguracji na komputerze.

Wszystkie lekcje w tym kursie

  1. Refaktoryzacja starszego kodu Objective-C
  2. Zarządzanie długiem technicznym
  3. Strategie długoterminowego utrzymania
  4. Pisanie testów charakterystyki przed refaktoryzacją
← Powrót do Objective-C iOS Development for Legacy & Enterprise Apps