2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

RxTestでVoidのEventをテストする

Posted at

RxTestのObserverを使ってVoidイベントの発火をテストするために以下のようなことをするとコンパイルに通りません。

// ViewModel.swift
struct ViewModel {
  let somethingOccurred: Signal<Void>
  ...
}

// ViewModel_Test.swift
let observer = testScheduler.createObserver(Void.self)
vm.somethingOccurred
  .emit(to: observer)
  .disposed(by: disposeBag)

expect(observer.events).to(equal([.next(100, ())])) // Compile Error!! Expression type Protocol type 'Any' cannot conform to 'Equatable' because only concrete types can conform to protocols

原因

なぜこうなるかというと、VoidがEquatableではないからです。

Voidはなにかというと空のtupleです。


public typealias Void = ()

tupleはEquatableではないのでVoidもそうなります。

対策


expect(observer.events).to(haveCount(1))
expect(observer.events[0].time).to(equal(100))

参考

2
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?