This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Testing | |
@testable import ExampleApp | |
@Suite("Example Service Test") struct ExampleServiceTest { | |
@Test("Restores from UserDefaults") func restoreValues() throws { | |
// setup | |
let classToBeStored = SomeClass(foo: "bar") | |
let dataToStore = try? JSONEncoder().encode(classToBeStored) | |
let mockUserDefault = MockUserDefault() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MockUserDefault: UserDefaults { | |
var persistedValue: Data? = nil | |
var persistedKey: String? = nil | |
override func set(_ value: Any?, forKey defaultName: String) { | |
persistedValue = value as? Data | |
persistedKey = defaultName | |
} | |
override func object(forKey defaultName: String) -> Any? { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ExampleService { | |
private let userDefaults: UserDefaults | |
var aStoredClass: SomeClass | |
init(userDefaults: UserDefaults) { | |
self.userDefaults = userDefaults | |
if let serialisedObject = self.userDefaults.object(forKey: "someKey") as? Data { | |
if let storedClass = try? JSONDecoder().decode(SomeClass.self, from: serialisedObject) { | |
self.aStoredClass = storedClass | |
} else { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Testing | |
import Foundation | |
@testable import ExampleApp | |
class MockUserDefaults: UserDefaults { | |
var persistedData: Data? = nil | |
var persistedKey: String? = nil | |
override func set(_ value: Any?, forKey defaultName: String) { | |
persistedData = value as? Data |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Testing | |
import Foundation | |
import SwiftData | |
@testable import ExampleApp | |
@Suite("Example Service Test") struct ExampleServiceTest { | |
let someOtherModelDescriptor = FetchDescriptor<SomeOtherModel>(sortBy: [SortDescriptor(\SomeOtherModel.created, order: .reverse)]) | |
@MainActor @Test("Adding SomeModel record when have SomeOtherModel record") func addRecordWithExisting() throws { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import SwiftUI | |
struct ContentView: View { | |
@EnvironmentObject private var exampleService: ExampleService | |
var body: some View { | |
List { | |
ForEach(exampleSerivce.someList, id: \.self) { listItem in | |
Text(listItem.name) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import SwiftUI | |
import SwiftData | |
@main | |
struct ExampleApp: App { | |
private let exampleService: ExampleService | |
let sharedModelContainer: ModelContainer | |
init() { | |
let schema = Schema([ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import SwiftData | |
// Ensure the service conforms to the ObservableObject protocol | |
class ExampleService: ObservableObject { | |
private let modelContext: ModelContext | |
@Published var someList = [SomeModel]() // The published decorator for properties that should trigger SwiftUI updates | |
// Pass the modelContext in to the initialiser so can use dependency injection during testing | |
init(modelContext: ModelContext) { | |
self.modelContext = modelContext |
NewerOlder