0Pricing
SwiftUI Academy · レッスン

ViewModelのユニットテスト

ビジネスロジックを単独でテストします。

「ViewModelのユニットテスト」はCoddyKit上の無料SwiftUI Academyレッスンです。 これはレッスン1/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはSwiftUI Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 SwiftUI Academyコースには全4レッスンが含まれています。

このレッスンの一部はまだ翻訳されておらず、英語で表示されています。

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. 🎉

よくある質問

「ViewModelのユニットテスト」レッスンは無料ですか?

はい。「ViewModelのユニットテスト」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、SwiftUI Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 SwiftUI Academyコースには全4レッスンが含まれています。

「ViewModelのユニットテスト」で何を学びますか?

ビジネスロジックを単独でテストします。 ブラウザで直接実行するハンズオンコードでSwiftUI Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。

SwiftUI Academyを始めるのに経験は必要ですか?

事前経験は必要ありません。CoddyKitのSwiftUI Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン1/4です。

「ViewModelのユニットテスト」レッスンにはどのくらい時間がかかりますか?

ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。

このSwiftUI Academyレッスンでコードを書いて実行できますか?

はい。すべてのSwiftUI Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。

このコースのすべてのレッスン

  1. ViewModelのユニットテスト
  2. 非同期コードのテスト
  3. XCUITestによるUIテスト
  4. スナップショットとプレビュー駆動テスト
← SwiftUI Academyに戻る