Created
April 20, 2021 19:35
-
-
Save PraveenKommuri/32477e9b751a6a02bd7d9b9fa7540adc to your computer and use it in GitHub Desktop.
CustomUserDefaults for writing unit tests. If we use suite name, it won't impact other test cases in same/other files.
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
//Initialization | |
var userDefaultsCustom: UserDefaults? | |
let userDefaultsSuiteName = "UserStateManagerTests" //You can pass class name here. | |
// In setUp() method | |
UserDefaults().removePersistentDomain(forName: userDefaultsSuiteName) | |
userDefaultsCustom = UserDefaults(suiteName: userDefaultsSuiteName) | |
// In test cases methods. | |
userDefaultsCustom?.setValue("SomeRandomValue", forKey: "SomeRandomKey") | |
userDefaultsCustom?.value(forKey: "SomeRandomKey") | |
// In tearDown() method. | |
userDefaultsCustom?.removePersistentDomain(forName: userDefaultsSuiteName) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment