Skip to content

Assert equality on collections: can the error message describe the difference between the two collections? #2149

Answered by bradwilson
prjm asked this question in General
Discussion options

You must be logged in to vote

This was recently fixed: f4be18e?w=1

Example:

[Fact]
public void ExactArraySize_DifferenceNearStart()
{
	var expectedMessage =
		"Assert.Equal() Failure" + Environment.NewLine +
		"              ↓ (pos 1)" + Environment.NewLine +
		"Expected: [1, 2, 3, 4, 5]" + Environment.NewLine +
		"Actual:   [1, 99, 3, 4, 5]" + Environment.NewLine +
		"              ↑ (pos 1)";

	var ex = Record.Exception(
		() => Assert.Equal(
			new List<int> { 1, 2, 3, 4, 5 },
			new List<int> { 1, 99, 3, 4, 5 }
		)
	);

	Assert.NotNull(ex);
	Assert.Equal<object>(expectedMessage, ex.Message);
}

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@prjm
Comment options

Answer selected by prjm
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants