// Example of mocking Keychain in a unit test
import XCTest
@testable import YourApp
class KeychainServiceMock: KeychainServiceProtocol {
var keychainValues = [String: String]()
func save(_ data: String, for key: String) -> Bool {
keychainValues[key] = data
return true
}
func retrieve(for key: String) -> String? {
return keychainValues[key]
}
func delete(for key: String) -> Bool {
keychainValues.removeValue(forKey: key)
return true
}
}
class KeychainTests: XCTestCase {
var keychainMock: KeychainServiceMock!
override func setUp() {
super.setUp()
keychainMock = KeychainServiceMock()
}
func testSaveAndRetrieve() {
keychainMock.save("testValue", for: "testKey")
let retrievedValue = keychainMock.retrieve(for: "testKey")
XCTAssertEqual(retrievedValue, "testValue")
}
func testDelete() {
keychainMock.save("testValue", for: "testKey")
keychainMock.delete(for: "testKey")
let retrievedValue = keychainMock.retrieve(for: "testKey")
XCTAssertNil(retrievedValue)
}
}
How do I avoid rehashing overhead with std::set in multithreaded code?
How do I find elements with custom comparators with std::set for embedded targets?
How do I erase elements while iterating with std::set for embedded targets?
How do I provide stable iteration order with std::unordered_map for large datasets?
How do I reserve capacity ahead of time with std::unordered_map for large datasets?
How do I erase elements while iterating with std::unordered_map in multithreaded code?
How do I provide stable iteration order with std::map for embedded targets?
How do I provide stable iteration order with std::map in multithreaded code?
How do I avoid rehashing overhead with std::map in performance-sensitive code?
How do I merge two containers efficiently with std::map for embedded targets?