I am observing for value change in userDefaults
userDefaults.rx.observe(Int.self, option.userDefaultsKey)
For unit testing I did userDefaultsMock, I am not getting how to emit when value is updated or set for those particular keys
override func setValue(_ value: Any?, forKey key: String) {
tempArray[key] = value
}
An Observable chain starts with a side effect (such as the observe(_:_:)
you posted) and ends with a side effect (whatever is in the subscribe.) Everything in-between the beginning of the chain and the end is logic [1]. Move that logic to a separate function so you can test it without the mock.
[1] A couple of exceptions to this rule are do()
calls and flatMap
calls. Everything else is logic that needs to be tested.
Actually I couldn't mock rx.observe
but did some work around to accomplish.
I used UserDefaults
but with suiteName
so not disturbing standard database
So this trick helped me as observable got notified.
I got idea from https://medium.com/@davidlin_98861/testing-userdefaults-cd86849fd896
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.