GithubHelp home page GithubHelp logo

nsuserdefaultsoverride's Introduction

NSUserDefaultsOverride

Override user defaults for testing.

How to use?

Just add NSUserDefaults+Override.h and NSUserDefaults+Override.m to your Xcode project. I recommend only adding it to the test target.

If you are using Swift, add #import "NSUserDefaults+Override.h" to the bridging header.

Objective C Example

NSLog(@"%d", [[NSUserDefaults standardUserDefaults] boolForKey:@"DoesItWork?"]); // outputs 0
[[NSUserDefaults standardUserDefaults] overrideValue:@(YES) forKey:@"DoesItWork?"];
NSLog(@"%d", [[NSUserDefaults standardUserDefaults] boolForKey:@"DoesItWork?"]); // outputs 1

Swift Example

print(UserDefaults.standard.bool(forKey: "ExampleKey")) // outputs false
UserDefaults.standard.overrideValue(true, forKey: "ExampleKey")
print(UserDefaults.standard.bool(forKey: "ExampleKey")) // outputs true

What is this good for?

It's hard to write unit tests for features that change according to user preferences.

You want to test all cases, so you need to change user defaults before running a test.

But if you change NSUserDefaults, other processes running at the same time will also be affected, and the changes will be written to disk. You need to remember to restore settings after the test completes, and hope that your test never crashes. And you can never run two tests at the same time.

So this is a way to override the values returned by NSUserDefaults temporarily.

How does it work?

It uses method swizzling to replace the standard implementations of objectForKey: etc.

Why use this instead of some other method?

  • You can continue to use NSUserDefaults in your code normally
  • No modifications to your app necessary (modifications only happen while testing)
  • Overrides are never persisted to disk and don't influence other processes

What's missing?

  • Does not send KVO notifications when a key is overridden

License

I've released this code under the MIT license. If you need a different license, let me know!

nsuserdefaultsoverride's People

Contributors

jakob avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.