0
observable.subscribe(onNext: { _ in
    somePrivateFunction()
})

What is the RxSwift way to test that when observable receives an event the somePrivateFunction actually gets called or not? Since the subscription and the function are in the same class I can't mock it.

Daniyal Raza
  • 352
  • 3
  • 13
  • The RxSwift library has an entire battery of tests to ensure that Observables work correctly. Including tests to ensure that when an observable emits a next event, the `onNext` closure gets called. If you want to test the RxSwift library, itself then I suggest you refer to those tests... However, I suspect that isn't what you are actually trying to test. Tells us what you really want to ensure and maybe we can help you. – Daniel T. Apr 12 '20 at 21:55

1 Answers1

1

You need to check if any logic is placed in a subscription that can block call of this function. If there is - it may be worth to extract it to a parameter (eg. filter) so that logic can be a part of stream itself.

I assume that observable (source) is injected/redirected from another component (if it's not, most probably it should be). To mock that signal you can use TestableObservable, you can read more here: http://adamborek.com/rxtests-rxactionsheet/

Last but not least - you need to identify what kind of action somePrivateFunction() does. If it's setting some external values - then you can test that outgoing connection from that function. If it sets some internal flags - you can test if value of that flag has changed.

Kamajabu
  • 596
  • 1
  • 5
  • 19