Is there a way to do unit test in userdefaults?

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

Answered by DTS Engineer in 403184022

Speaking personally, I wouldn’t write a unit test for that code. When you spend time writing test code, you want to focus on testing things that are likely to fail. That’s not this code. IMO, you’d be better off spending that time writing new code, or writing better tests for the more complex parts of your app.

Having said that, there’s three ways you can unit test this:

  • Round trip (A)

  • Peek in

    UserDefaults
    (B)
  • Decouple the code from the

    UserDefaults.standard
    singleton (C)

For A, you’d write a ‘random’ value and then read it back to see if you get back what you wrote.

For B, you can have your test code call

UserDefaults
to see if the right value was written. Remember that unit tests run in the same process as the code they’re testing, so can access the same defaults.

For C, you’d introduce an abstraction for

UserDefaults
into the code so the test can confirm that it did the right thing at that abstraction boundary. This is massive overkill IMO.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"
Accepted Answer

Speaking personally, I wouldn’t write a unit test for that code. When you spend time writing test code, you want to focus on testing things that are likely to fail. That’s not this code. IMO, you’d be better off spending that time writing new code, or writing better tests for the more complex parts of your app.

Having said that, there’s three ways you can unit test this:

  • Round trip (A)

  • Peek in

    UserDefaults
    (B)
  • Decouple the code from the

    UserDefaults.standard
    singleton (C)

For A, you’d write a ‘random’ value and then read it back to see if you get back what you wrote.

For B, you can have your test code call

UserDefaults
to see if the right value was written. Remember that unit tests run in the same process as the code they’re testing, so can access the same defaults.

For C, you’d introduce an abstraction for

UserDefaults
into the code so the test can confirm that it did the right thing at that abstraction boundary. This is massive overkill IMO.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Thanks @Eskimo

Is there a way to do unit test in userdefaults?
 
 
Q