6

I have 2 functions where one basically retrieves string in User Defaults and another writes the string in User Defaults. I'm not able to understand that should I do the unit test or no? and I'm pretty new to the concept for Unit testing.

I scoured the internet for testing user defaults but I was advised to mock the test in the end.

My question is If there is a way to test User Defaults what is the best way to do it?

Constants and Structs

let defaults = UserDefaults.standard
let defaultinformation = "ABCDEFG"

struct Keys {
    static let Information = "Information"
}

Function where saves Information

func SetDefaultInformation() {
    defaults.set(defaultinformation, forKey: Keys.Information)

}

Function where retrieves Information

func checkForInformation() -> String {
    let Information = defaults.value(forKey: Keys.Information) as? String ?? ""
    return Information
}

Thanks in Advance

Kane D
  • 63
  • 1
  • 4

2 Answers2

9

should I do the unit test or no

No. You know what UserDefaults does and you know how it works and that it works. It is ridiculous to test Apple's code. Testing defaults.set is just as silly; you know exactly what it does and you know that it will do it.

What you want to test is your code: not your retrieval from UserDefaults per se, but your response to that retrieval. Give yourself methods such that you can see what you do when information is a String and what you do when information is nil. As you've been told, a trivial mock can supply the back end, playing the part of UserDefaults and giving back different sorts of result. Just don't let your tests involve the real UserDefaults.

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • Hey @matt I have a requirement to do unit test on these functions basically add random strings and save and check if save. I know it's not needed and it makes no sense to test apple given function is there any other way to test other than mock test. I'm really new to testing. It would be great if you can shed some light on this.Thank You. – Kane D Jan 22 '20 at 02:34
  • Sure, well, do you understand what a mock is? You can, for example, create a protocol to which both your class and UserDefaults conforms, and that requires `set(_:forKey:)` and `value(forKey:)`. Now it's easy for your test to interpose your class instead of UserDefaults. As I said in my answer, "a trivial mock can supply the back end". – matt Jan 22 '20 at 02:38
  • No I don't understand mock. I was not able to wrap my head around it. Appreciate the help so far. Thanks – Kane D Jan 22 '20 at 02:52
  • 2
    This is a good one to start: https://developer.apple.com/videos/play/wwdc2018/417 – matt Jan 22 '20 at 03:13
4
class ViewModel {
     func saveUserName(name: String, userDefaults: UserDefaults = UserDefaults.standard) {
            userDefaults.set(name, forKey: "username")
     }
}
class MockUserDefault: UserDefaults {
    var persistedUserName: String? = nil
    var persistenceKey: String? = nil
        
    override func set(_ value: Any?, forKey defaultName: String) {
        persistedUserName = value as? String
        persistenceKey = defaultName
    }
}
    
func testSavingUserName() {
    let viewModel = ViewModel()
    let mockUserDefaults = MockUserDefault()
    viewModel.saveUserName(name: "Oliver", userDefaults: mockUserDefaults)
        
    XCTAssertEqual("Oliver", mockUserDefaults.persistedUserName)
    XCTAssertEqual("username", mockUserDefaults.persistenceKey)
}