Assert.That on a null non-generic IEnumerable throws ArgumentNullException, so IsNull() cannot pass

Open Beginner friendly
#7,698 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
72/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Active
Tech stack
csharp
Domain
testing

Research direction

Start at the Assert.That overload for non-generic IEnumerable and compare its null handling with the generic IEnumerable<T> and object overloads. Check the related IsNull() and other assertion paths; add coverage for null and non-null values. Done when IsNull() passes for null IEnumerable and other assertions report assertion failures instead of ArgumentNullException.

Written by the indexing model from the issue text.

Description

bug P2

Assert.That(x).IsNull() throws ArgumentNullException when x is a null non-generic IEnumerable, so the assertion can never pass. With TUnit 1.79.0:

IEnumerable? renderings = null;
await Assert.That(renderings).IsNull();   // ArgumentNullException: Value cannot be null. (Parameter 'source')

The generic and object overloads accept the same null value:

IEnumerable<int>? items = null;
await Assert.That(items).IsNull();        // passes

object? value = null;
await Assert.That(value).IsNull();        // passes

The overload taking a non-generic IEnumerable also declares its parameter non-nullable, so the compiler warns (CS8604) at the call above. Casting the value to object? makes the assertion pass.

Expected: IsNull() passes for a null non-generic IEnumerable, as it does for IEnumerable<T>. Other assertions on a null value should fail with an assertion failure rather than ArgumentNullException.

Found while migrating an xUnit suite, where Assert.Null(renderings) on a non-generic IEnumerable passed.

Dominant language
C#
Stars
4k
Forks
139
Avg merge
7h 16m
Merged PRs (30d)
355

Getting set up

Open in Codespaces

Starts the project's dev container in your browser, under your own GitHub account.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from thomhurst/TUnit

All issues in thomhurst/TUnit

Similar issues

More C# issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.