0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

[Swift]UserDefaultsを使用したクラスのテスト方法

Posted at

始めに

UserDefaultsを使用したクラスのテスト方法について調べたいと思います。

実装

@testable import Project_Code
import Foundation
import Testing

class AppThemePresenterTests {
    // MARK: Lifecycle

    init() {
        userDefaults = UserDefaults(suiteName: AppThemePresenterTests.suiteName)!
    }

    deinit {
        userDefaults.removePersistentDomain(forName: AppThemePresenterTests.suiteName)
    }

    // MARK: Internal

    static let suiteName: String = "Test"

    @Test func updateAppTheme() {
        // Given
        let presenter = AppThemePresenter(userDefaults: userDefaults)
        let appTheme = AppTheme.light
        // When
        presenter.updateAppTheme(appTheme: appTheme)
        // Then
        #expect(presenter.selectedAppTheme == appTheme)
        #expect(userDefaults.appTheme == appTheme)
    }

    // MARK: Private

    private let userDefaults: UserDefaults
}

Point

終わりに

設計時にDIを意識しながらコードを書くことを、もう少し心がけたいと思いました。

参考

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?