MockUserDefaults

2.4.0

Mocking UserDefaults for tests
417-72KI/MockUserDefaults

What's New

2.4.0

2024-04-14T15:26:00Z

Important

  1. Now requires Swift 5.7 or above
  2. UserDefaults.mocked in Swift has been deprecated and will be renamed to UserDefults.mock.

What's Changed

Full Changelog: 2.3.0...2.4.0

MockUserDefaults

Test GitHub release Platform Version GitHub license

MockUserDefaults is a simple replacement for NSUserDefaults/UserDefaults to make test easier.

Installation

SwiftPM (recommended)

.package(url: "https://github.com/417-72KI/MockUserDefaults.git", from: "2.4.0"),

CocoaPods

pod 'MockUserDefaults'

Usage

Objective-C

NSUserDefaults *userDefaults = [NSUserDefaults mockedUserDefaults];
[userDefaults setObject:@"string" forKey:@"key"];

XCTAssertEqualObjects([userDefaults objectForKey:@"key"], @"string");

Swift

let userDefaults: UserDefaults = .mock
userDefaults.set("string", forKey: "key")

XCTAssertEqual(userDefaults.string(forKey: "key"), "string")

Demo

Create project

First time

make init_demo_app

After the first time

make demo_app

Usage

  1. open MockUserDefaultsDemo.xcworkspace
  2. Build and run app (⌘+R) in iPhone / iPad Simulator
  3. Add any keys and values
  4. Run test (⌘+U) in same simulator
  5. Re-run app (⌘+R) in same simulator
  6. You can see that the app isn't affected by the test.

Description

  • Swift Tools 5.7.0
View More Packages from this Author

Dependencies

  • None
Last updated: Fri Oct 18 2024 06:05:27 GMT-0900 (Hawaii-Aleutian Daylight Time)