-
-
Notifications
You must be signed in to change notification settings - Fork 366
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Consider making DefaultsAdapter a class #268
Comments
Setters can be declared as |
Hey @alexito4 - I just merged two PRs, one that adds |
Oh that sounds cool! I would made a task to check out the update. Thanks 😉 |
Thank you for this @sunshinejr! In case anyone is interested, it took me a while but I figured out how to use SwiftyUserDefaults in the app and mock it in my tests. I'm using TCA which gives me an Then, I have this convenience extension method that I call on my passed import Foundation
import SwiftyUserDefaults
extension UserDefaults {
/// Returns a SwiftyUserDefaults enhanced object that can be used like the `Defaults` object in the SwiftyUserDefaults documentation.
var swifty: DefaultsAdapter<DefaultsKeys> {
DefaultsAdapter<DefaultsKeys>(defaults: self, keyStore: .init())
}
} Then, instead of In my test target, I have another extension for convenience: import Foundation
extension UserDefaults {
static var test = UserDefaults(suiteName: "com.my.app.tests")!
} This allows me to pass in class LoginTests: XCTestCase {
override func setUp() {
UserDefaults.test.removeAll()
}
func testForgotPassword() {
let store = TestStore(initialState: .init(), reducer: loginReducer, environment: .init(userDefaults: .test))
UserDefaults.test.swifty.isFirstAppStart = true
// my tests expecting `isFirstAppStart` to be `true`
}
// ...
} I hope this helps someone out there! |
@Jeehut: I would recommend you use removePersistentDomain as well. As is, I believe some tests might impact others eventually. https://www.swiftbysundell.com/tips/avoiding-mocking-userdefaults/ |
The current design of DefaultsAdapter relies on it being used via the global variable Defaults which is a var. But using that global variable is not ideal for testing so I'm trying to use this library with typical DI.
Once doing that and storing a DefaultsAdapter as a property to be used things don't work as smoothly as expected. The problem is that the adapter is a struct and thus you can't mutate it easily. And in reality is not mutating any values so it's kind of misleading.
Changing it to a class shouldn't have any impact and would allow dynamic member lookup to be used in more places.
The text was updated successfully, but these errors were encountered: