title: How to test a function that gets into the main thread in Swift with RxSwift and XCTest?
body: <p><strong>I came across this problem when testing my View:</strong></p>
  
  <p>In my ViewModel I call to an asynchronous operation and when the response arrives, I use a <code>PublishSubject</code> to produce a change in my View. In my View, I call <code>DispatchQueue.main.async</code> in order to hide or show a button.</p>
  
  <p>ViewModel</p>
  
  <pre><code>let refreshButtons = PublishSubject&lt;Bool&gt;(true)
  refreshButtons.onNext(true)
  </code></pre>
  
  <p>View</p>
  
  <pre><code>model.refreshButtons.asObservable()
                  .subscribe(onNext: {
                      [unowned self] success in
                      self.updateButtons(success)
                  })
                  .addDisposableTo(disposable)
  
  private func updateButtons(_ show:Bool) {
     DispatchQueue.main.async{
        button.isHidden = !show
     }
  }
  </code></pre>
  
  <p>Now I don't know how to unit test that <code>refreshButtons.onNext(true)</code> will hide or show my button. </p>
  
  <p>The solutions I can think of are:</p>
  
  <ul>
  <li>Overriding the method and having an async expectation, but for that I need to make the method public, what I don't want, or</li>
  <li>Dispatching the main queue in my ViewModel and not in the view, what it sounds odd to me, but might me ok.</li>
  </ul>
  
  <p>How can I solve this?
  Thank you in advance.</p>