RxSwift Observing test cases on mocked UserDefaults

Viewed 349

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
    }
2 Answers

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.

Related