expectRxException static method

void expectRxException(
  1. void operation(),
  2. String expectedMessage, {
  3. Type? expectedOriginalError,
})

Test that an operation throws a specific RxException

Implementation

static void expectRxException(
  void Function() operation,
  String expectedMessage, {
  Type? expectedOriginalError,
}) {
  try {
    operation();
    throw Exception('Expected RxException but none was thrown');
  } catch (e) {
    if (e is! RxException) {
      throw Exception('Expected RxException but got ${e.runtimeType}: $e');
    }

    if (!e.message.contains(expectedMessage)) {
      throw Exception(
          'Expected message containing "$expectedMessage" but got "${e.message}"');
    }

    if (expectedOriginalError != null &&
        e.originalError?.runtimeType != expectedOriginalError) {
      throw Exception(
          'Expected original error of type $expectedOriginalError but got ${e.originalError?.runtimeType}');
    }
  }
}