0Pricing
SwiftUI Academy · Lekcja

Testy jednostkowe ViewModeli

Testuj logikę biznesową niezależnie

Testy jednostkowe ViewModeli to bezpłatna lekcja SwiftUI Academy na CoddyKit. To lekcja 1 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 SwiftUI Academy, a Twój postęp synchronizuje się między webem a aplikacją CoddyKit. Kurs SwiftUI Academy zawiera 4 lekcji w sumie.

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

Why Test the ViewModel?

Your ViewModel holds the logic, so it is the perfect place to test. You can check behavior without ever launching the full UI. 🧪

Logic Lives Apart from Views

Because a ViewModel is a plain class, you can create one in a test and call its methods directly. This keeps your tests fast and focused.

Meet XCTest

Apple's testing framework is XCTest. You import it and write test classes that Xcode runs for you with a single keystroke.

import XCTest
@testable import MyApp

A Test Class

A XCTestCase subclass groups related tests. Every test method name must start with the word test so Xcode discovers it automatically.

final class CounterVMTests: XCTestCase {
}

Arrange, Act, Assert

A clean test follows three steps: arrange the objects, act by calling a method, then assert the result is what you expected.

Create the ViewModel

Start by making a fresh instance of the ViewModel inside your test. A new object per test keeps each one independent and reliable.

let vm = CounterViewModel()

Call a Method

Now act on it. Call the method you want to verify, just like your view would when a button is tapped. ➡️

vm.increment()

Assert with XCTAssertEqual

Use XCTAssertEqual to confirm a value matches what you expect. If it differs, the test fails with a clear message.

XCTAssertEqual(vm.count, 1)

Testing Booleans

For true or false results, reach for XCTAssertTrue and XCTAssertFalse. They read clearly and document your intent.

XCTAssertTrue(vm.isEmpty)

Test Edge Cases

Good tests cover more than the happy path. Check empty input, limits, and unexpected values so bugs surface before your users do.

Run with One Shortcut

Press Command-U in Xcode to run every test. Green checkmarks mean your logic still works after each change you make.

Quick Check

Which assertion confirms two values are equal?

Recap

You learned to test a ViewModel in isolation: create it, call a method, then assert the result with XCTest. Fast, focused, dependable. 🎉

Często zadawane pytania

Czy lekcja „Testy jednostkowe ViewModeli” jest bezpłatna?

Tak — pełny tekst „Testy jednostkowe ViewModeli” 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 SwiftUI Academy, przejdź na CoddyKit PRO. Kurs SwiftUI Academy zawiera 4 lekcji w sumie.

Co nauczysz się w „Testy jednostkowe ViewModeli”?

Testuj logikę biznesową niezależnie Ćwiczysz SwiftUI Academy 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ąć SwiftUI Academy?

Nie wymagamy żadnego doświadczenia. SwiftUI Academy 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 1 z 4.

Ile czasu zajmuje lekcja „Testy jednostkowe ViewModeli”?

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 SwiftUI Academy?

Tak. Każda lekcja SwiftUI Academy 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. Testy jednostkowe ViewModeli
  2. Testowanie kodu asynchronicznego
  3. Testy interfejsu z XCUITest
  4. Testowanie oparte na migawkach i podglądach
← Powrót do SwiftUI Academy