dotnet/reactive · error · ArgumentNullException

Value cannot be null. (Parameter 'expected')

Error message

Value cannot be null. (Parameter 'expected')

What it means

ReactiveAssert.AreElementsEqual(expected, actual, comparer, message) throws ArgumentNullException with parameter name 'expected' when the expected IObservable<T> is null. This is an upfront argument validation in the Rx testing helper before comparing materialized notifications.

Solutions

  1. Fix the test to pass a non-null expected observable (e.g. scheduler.CreateColdObservable(...) or Observable.Range(...)).
  2. Guard the code that produces the expected sequence so it never yields null.
  3. If null is a legitimate outcome, assert null-ness separately instead of calling AreElementsEqual.

Example fix

// before
IObservable<int> expected = MaybeCreate();
ReactiveAssert.AreElementsEqual(expected, actual, comparer, "msg");
// after
IObservable<int> expected = MaybeCreate() ?? Observable.Empty<int>();
ReactiveAssert.AreElementsEqual(expected, actual, comparer, "msg");
Defensive patterns

Strategy: validation

Validate before calling

if (expected == null)
    throw new InvalidOperationException("Test misconfigured: expected observable is null");
ReactiveAssert.AreElementsEqual(expected, actual, comparer, "msg");

Type guard

bool ok = expected is IObservable<int> nonNull;
if (!ok) failFast();

Try / catch

try
{
    ReactiveAssert.AreElementsEqual(expected, actual, comparer, "sequences differ");
}
catch (ArgumentNullException ex) when (ex.ParamName == "expected")
{
    Assert.Fail("Expected observable was null — fixture setup bug");
}

Prevention

When it happens

Trigger: Calling ReactiveAssert.AreElementsEqual with a null expected observable, e.g. a test fixture returning null because a service/factory failed silently.

Common situations: Unit tests where the 'expected' hot/cold observable was never assigned, a mock returned null instead of a scheduler-created observable, or refactored test helpers broke observable creation.

Related errors


AI-assisted analysis of dotnet/reactive@94b5d5ab91 (2026-09-15). Data as JSON: /api/errors/0c42e4987b5a2755. Report an issue: GitHub.

Appendix: source

Thrown at Rx.NET/Source/src/Microsoft.Reactive.Testing/ReactiveAssert.cs:189

            }

            AreElementsEqual(expected.Materialize().ToEnumerable(), actual.Materialize().ToEnumerable(), message);
        }

        /// <summary>
        /// Asserts that both observable sequences have equal length and equal elements.
        /// </summary>
        /// <typeparam name="T">The type of the elements in the sequence.</typeparam>
        /// <param name="expected">Expected sequence.</param>
        /// <param name="actual">Actual sequence to compare against the expected one.</param>
        /// <param name="comparer">Comparer used to check for equality.</param>
        /// <param name="message">Error message for assert failure.</param>
        /// <exception cref="ArgumentNullException"><paramref name="expected"/> or <paramref name="actual"/> is null.</exception>
        public static void AreElementsEqual<T>(IObservable<T> expected, IObservable<T> actual, IEqualityComparer<T> comparer, string message)
        {
            if (expected == null)
            {
                throw new ArgumentNullException(nameof(expected));
            }

            if (actual == null)
            {
                throw new ArgumentNullException(nameof(actual));
            }

            AreElementsEqual(expected.Materialize().ToEnumerable(), actual.Materialize().ToEnumerable(), new NotificationComparer<T>(comparer), message);
        }

        /// <summary>
        /// Asserts that the given action throws an exception of the type specified in the generic parameter, or a subtype thereof.
        /// </summary>
        /// <typeparam name="TException">Type of the exception to check for.</typeparam>
        /// <param name="action">Action to run.</param>
        /// <exception cref="ArgumentNullException"><paramref name="action"/> is null.</exception>
        public static void Throws<TException>(Action action) where TException : Exception
        {

View on GitHub (pinned to 94b5d5ab91)