1

How would I test a method that looks like this?

// in unit test suite

var afterSubscribeClosureEntered = false
viewModel.afterSubscribe = { res in
     afterSubscribeClosureEntered = true
}

viewModel.someMethod()
expect(afterSubscribeClosureEntered).to(beTrue()) // if I used toEventually, it will fail in unit test (time out, unresponsive main loop)

// view model

var afterSubscribe: ((Bool) -> Void)?

func someMethod() {

   observableOne() // method that returns Observable<T>
     .flatMap(observableTwo()) // returns Observable<T>
     .subscribe(onNext: { val in
         // some operation
         self.afterSubscribe?(val)
     })
     .disposed(by: disposeBag)
}

I tried using toEventually but that breaks in Fastlane (time out, main loop unresponsive). The test suite succeed in XCode test though. I don't want to inject a completionHandler in someMethod.Is there a way to test this without using toEventually and waitUntil? Thanks alot!

Kevin You
  • 11
  • 4
  • 1
    What exactly do you want to test? It seems to me that there's no logic in the function that can be unit tested. It's all side effects. Also, your code doesn't compile. You can't possibly test code that doesn't even compile. – Daniel T. Jan 05 '21 at 12:16
  • @DanielT. Sorry for the vague question, I've added more code description. Hope it's clearer. Basically I have a view model that has several optional closure as property, so the view controller can assign some operation to it. Please tell me if I can add more to describe my question. – Kevin You Jan 05 '21 at 18:27
  • The code inside your `someMethod()` doesn't compile, therefore it cannot be tested. Post code that _at least_ compiles. That's the first requirement before any unit test can be written. – Daniel T. Jan 05 '21 at 19:09

0 Answers0