Skip to content

Commit f6c65c1

Browse files
authored
Change HaveCount assertion message order to state number before dumpi… (#1760)
Change HaveCount assertion message order to state number before dumping object contents
1 parent c55a7a4 commit f6c65c1

9 files changed

+28
-26
lines changed

Src/FluentAssertions/Collections/GenericCollectionAssertions.cs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1230,8 +1230,8 @@ public AndConstraint<TAssertions> HaveCount(int expected, string because = "", p
12301230
.ForCondition(actualCount == expected)
12311231
.BecauseOf(because, becauseArgs)
12321232
.FailWith(
1233-
"Expected {context:collection} {0} to contain {1} item(s){reason}, but found {2}.",
1234-
Subject, expected, actualCount);
1233+
"Expected {context:collection} to contain {0} item(s){reason}, but found {1}: {2}.",
1234+
expected, actualCount, Subject);
12351235
}
12361236

12371237
return new AndConstraint<TAssertions>((TAssertions)this);
@@ -1269,8 +1269,8 @@ public AndConstraint<TAssertions> HaveCount(Expression<Func<int, bool>> countPre
12691269
{
12701270
Execute.Assertion
12711271
.BecauseOf(because, becauseArgs)
1272-
.FailWith("Expected {context:collection} {0} to have a count {1}{reason}, but count is {2}.",
1273-
Subject, countPredicate.Body, actualCount);
1272+
.FailWith("Expected {context:collection} to have a count {0}{reason}, but count is {1}: {2}.",
1273+
countPredicate.Body, actualCount, Subject);
12741274
}
12751275
}
12761276

@@ -1299,7 +1299,7 @@ public AndConstraint<TAssertions> HaveCountGreaterThanOrEqualTo(int expected, st
12991299
.Then
13001300
.Given(subject => subject.Count())
13011301
.ForCondition(actualCount => actualCount >= expected)
1302-
.FailWith("but found {0}.", actualCount => actualCount)
1302+
.FailWith("but found {0}: {1}.", actualCount => actualCount, _ => Subject)
13031303
.Then
13041304
.ClearExpectation();
13051305

@@ -1331,7 +1331,7 @@ public AndConstraint<TAssertions> HaveCountGreaterThan(int expected, string beca
13311331
.Then
13321332
.Given(subject => subject.Count())
13331333
.ForCondition(actualCount => actualCount > expected)
1334-
.FailWith("but found {0}.", actualCount => actualCount)
1334+
.FailWith("but found {0}: {1}.", actualCount => actualCount, _ => Subject)
13351335
.Then
13361336
.ClearExpectation();
13371337

@@ -1360,7 +1360,7 @@ public AndConstraint<TAssertions> HaveCountLessThanOrEqualTo(int expected, strin
13601360
.Then
13611361
.Given(subject => subject.Count())
13621362
.ForCondition(actualCount => actualCount <= expected)
1363-
.FailWith("but found {0}.", actualCount => actualCount)
1363+
.FailWith("but found {0}: {1}.", actualCount => actualCount, _ => Subject)
13641364
.Then
13651365
.ClearExpectation();
13661366

@@ -1392,7 +1392,7 @@ public AndConstraint<TAssertions> HaveCountLessThan(int expected, string because
13921392
.Then
13931393
.Given(subject => subject.Count())
13941394
.ForCondition(actualCount => actualCount < expected)
1395-
.FailWith("but found {0}.", actualCount => actualCount)
1395+
.FailWith("but found {0}: {1}.", actualCount => actualCount, _ => Subject)
13961396
.Then
13971397
.ClearExpectation();
13981398

@@ -2431,7 +2431,7 @@ public AndConstraint<TAssertions> NotEqual(IEnumerable<T> unexpected, string bec
24312431
.Then
24322432
.Given(subject => subject.ConvertOrCastToCollection())
24332433
.ForCondition(actualItems => !actualItems.SequenceEqual(unexpected))
2434-
.FailWith("Did not expect collections {0} and {1} to be equal{reason}.", actualItems => unexpected, actualItems => actualItems);
2434+
.FailWith("Did not expect collections {0} and {1} to be equal{reason}.", _ => unexpected, actualItems => actualItems);
24352435

24362436
return new AndConstraint<TAssertions>((TAssertions)this);
24372437
}
@@ -2493,7 +2493,7 @@ public AndConstraint<TAssertions> NotHaveSameCount<TExpectation>(IEnumerable<TEx
24932493
.ForCondition(subject => !ReferenceEquals(subject, otherCollection))
24942494
.FailWith(
24952495
"Expected {context:collection} {0} to not have the same count as {1}{reason}, but they both reference the same object.",
2496-
subject => subject, subject => otherCollection)
2496+
subject => subject, _ => otherCollection)
24972497
.Then
24982498
.Given(subject => (actual: subject.Count(), expected: otherCollection.Count()))
24992499
.ForCondition(count => count.actual != count.expected)
@@ -2532,13 +2532,13 @@ public AndConstraint<TAssertions> NotIntersectWith(IEnumerable<T> otherCollectio
25322532
.ForCondition(subject => !ReferenceEquals(subject, otherCollection))
25332533
.FailWith(
25342534
"Did not expect {context:collection} {0} to intersect with {1}{reason}, but they both reference the same object.",
2535-
subject => subject, subject => otherCollection)
2535+
subject => subject, _ => otherCollection)
25362536
.Then
25372537
.Given(subject => subject.Intersect(otherCollection))
25382538
.ForCondition(sharedItems => !sharedItems.Any())
25392539
.FailWith(
25402540
"Did not expect {context:collection} to intersect with {0}{reason}, but found the following shared items {1}.",
2541-
sharedItems => otherCollection, sharedItems => sharedItems);
2541+
_ => otherCollection, sharedItems => sharedItems);
25422542

25432543
return new AndConstraint<TAssertions>((TAssertions)this);
25442544
}

Tests/FluentAssertions.Specs/Collections/CollectionAssertionSpecs.HaveCount.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public void When_collection_has_a_count_that_is_different_from_the_number_of_ite
4646

4747
// Assert
4848
action.Should().Throw<XunitException>()
49-
.WithMessage("Expected collection*1*2*3* to contain 4 item(s) because we want to test the failure message, but found 3.");
49+
.WithMessage("Expected collection to contain 4 item(s) because we want to test the failure message, but found 3: {1, 2, 3}.");
5050
}
5151

5252
[Fact]
@@ -70,7 +70,7 @@ public void When_collection_has_a_count_that_not_matches_the_predicate_it_should
7070

7171
// Assert
7272
act.Should().Throw<XunitException>().WithMessage(
73-
"Expected collection {1, 2, 3} to have a count (c >= 4) because a minimum of 4 is required, but count is 3.");
73+
"Expected collection to have a count (c >= 4) because a minimum of 4 is required, but count is 3: {1, 2, 3}.");
7474
}
7575

7676
[Fact]

Tests/FluentAssertions.Specs/Collections/CollectionAssertionSpecs.HaveCountGreaterThan.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public void When_collection_has_a_count_greater_than_the_number_of_items_it_shou
4747

4848
// Assert
4949
action.Should().Throw<XunitException>()
50-
.WithMessage("*more than*3*because we want to test the failure message*3*");
50+
.WithMessage("Expected collection to contain more than 3 item(s) because we want to test the failure message, but found 3: {1, 2, 3}.");
5151
}
5252

5353
[Fact]

Tests/FluentAssertions.Specs/Collections/CollectionAssertionSpecs.HaveCountGreaterThanOrEqualTo.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public void When_collection_has_a_count_greater_than_or_equal_to_the_number_of_i
4646

4747
// Assert
4848
action.Should().Throw<XunitException>()
49-
.WithMessage("*at least*4*because we want to test the failure message*3*");
49+
.WithMessage("Expected collection to contain at least 4 item(s) because we want to test the failure message, but found 3: {1, 2, 3}.");
5050
}
5151

5252
[Fact]

Tests/FluentAssertions.Specs/Collections/CollectionAssertionSpecs.HaveCountLessThan.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public void When_collection_has_a_count_less_than_the_number_of_items_it_should_
4646

4747
// Assert
4848
action.Should().Throw<XunitException>()
49-
.WithMessage("*fewer than*3*because we want to test the failure message*3*");
49+
.WithMessage("Expected collection to contain fewer than 3 item(s) because we want to test the failure message, but found 3: {1, 2, 3}.");
5050
}
5151

5252
[Fact]

Tests/FluentAssertions.Specs/Collections/CollectionAssertionSpecs.HaveCountLessThanOrEqualTo.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public void When_collection_has_a_count_less_than_or_equal_to_the_number_of_item
4646

4747
// Assert
4848
action.Should().Throw<XunitException>()
49-
.WithMessage("*at most*2*because we want to test the failure message*3*");
49+
.WithMessage("Expected collection to contain at most 2 item(s) because we want to test the failure message, but found 3: {1, 2, 3}.");
5050
}
5151

5252
[Fact]

Tests/FluentAssertions.Specs/Collections/GenericCollectionAssertionOfStringSpecs.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1075,7 +1075,7 @@ public void
10751075
// Assert
10761076
action.Should().Throw<XunitException>()
10771077
.WithMessage(
1078-
"Expected collection {\"one\", \"two\", \"three\"} to contain 4 item(s) because we want to test the failure message, but found 3.");
1078+
"Expected collection to contain 4 item(s) because we want to test the failure message, but found 3: {\"one\", \"two\", \"three\"}.");
10791079
}
10801080

10811081
[Fact]
@@ -1089,7 +1089,7 @@ public void When_collection_has_a_count_that_not_matches_the_predicate_it_should
10891089

10901090
// Assert
10911091
act.Should().Throw<XunitException>().WithMessage(
1092-
"Expected collection {\"one\", \"two\", \"three\"} to have a count (c >= 4) because a minimum of 4 is required, but count is 3.");
1092+
"Expected collection to have a count (c >= 4) because a minimum of 4 is required, but count is 3: {\"one\", \"two\", \"three\"}.");
10931093
}
10941094

10951095
[Fact]

Tests/FluentAssertions.Specs/Collections/GenericDictionaryAssertionSpecs.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ public void
172172

173173
// Assert
174174
action.Should().Throw<XunitException>()
175-
.WithMessage("Expected dictionary {[1] = \"One\", [2] = \"Two\", [3] = \"Three\"} to contain 4 item(s) because we want to test the failure message, but found 3.");
175+
.WithMessage("Expected dictionary to contain 4 item(s) because we want to test the failure message, but found 3: {[1] = \"One\", [2] = \"Two\", [3] = \"Three\"}.");
176176
}
177177

178178
[Fact]
@@ -206,7 +206,7 @@ public void When_dictionary_has_a_count_that_not_matches_the_predicate_it_should
206206

207207
// Assert
208208
act.Should().Throw<XunitException>().WithMessage(
209-
"Expected dictionary {[1] = \"One\", [2] = \"Two\", [3] = \"Three\"} to have a count (c >= 4) because a minimum of 4 is required, but count is 3.");
209+
"Expected dictionary to have a count (c >= 4) because a minimum of 4 is required, but count is 3: {[1] = \"One\", [2] = \"Two\", [3] = \"Three\"}.");
210210
}
211211

212212
[Fact]
@@ -378,7 +378,7 @@ public void When_dictionary_has_a_count_greater_than_the_number_of_items_it_shou
378378

379379
// Assert
380380
action.Should().Throw<XunitException>()
381-
.WithMessage("*more than*3*because we want to test the failure message*3*");
381+
.WithMessage("Expected dictionary to contain more than 3 item(s) because we want to test the failure message, but found 3: {[1] = \"One\", [2] = \"Two\", [3] = \"Three\"}.");
382382
}
383383

384384
[Fact]
@@ -447,7 +447,7 @@ public void When_dictionary_has_a_count_greater_than_or_equal_to_the_number_of_i
447447

448448
// Assert
449449
action.Should().Throw<XunitException>()
450-
.WithMessage("*at least*4*because we want to test the failure message*3*");
450+
.WithMessage("Expected dictionary to contain at least 4 item(s) because we want to test the failure message, but found 3: {[1] = \"One\", [2] = \"Two\", [3] = \"Three\"}.");
451451
}
452452

453453
[Fact]
@@ -516,7 +516,7 @@ public void When_dictionary_has_a_count_less_than_the_number_of_items_it_should_
516516

517517
// Assert
518518
action.Should().Throw<XunitException>()
519-
.WithMessage("*fewer than*3*because we want to test the failure message*3*");
519+
.WithMessage("Expected dictionary to contain fewer than 3 item(s) because we want to test the failure message, but found 3: {[1] = \"One\", [2] = \"Two\", [3] = \"Three\"}.");
520520
}
521521

522522
[Fact]
@@ -585,7 +585,7 @@ public void When_dictionary_has_a_count_less_than_or_equal_to_the_number_of_item
585585

586586
// Assert
587587
action.Should().Throw<XunitException>()
588-
.WithMessage("*at most*2*because we want to test the failure message*3*");
588+
.WithMessage("Expected dictionary to contain at most 2 item(s) because we want to test the failure message, but found 3: {[1] = \"One\", [2] = \"Two\", [3] = \"Three\"}.");
589589
}
590590

591591
[Fact]

docs/_pages/releases.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@ sidebar:
1212
### What's New
1313
* Adding `ThatAreAsync()` and `ThatAreNotAsync()` for filtering in method assertions - [#1725](https://github.com/fluentassertions/fluentassertions/pull/1725)
1414
* Adding `ThatAreVirtual()` and `ThatAreNotVirtual()` for filtering in method assertions - [#1744](https://github.com/fluentassertions/fluentassertions/pull/1744)
15+
* Adding collection content to assertion messages for `HaveCountGreaterThan()`, `HaveCountGreaterThanOrEqualTo()`, `HaveCountLessThan()` and `HaveCountLessThanOrEqualTo()` - [#1760](https://github.com/fluentassertions/fluentassertions/pull/1760)
1516
### Fixes
1617
* Prevent multiple enumeration of `IEnumerable`s in parameter-less `ContainSingle()` - [#1753](https://github.com/fluentassertions/fluentassertions/pull/1753)
18+
* Change `HaveCount()` assertion message order to state expected and actual collection count before dumping its content` - [#1760](https://github.com/fluentassertions/fluentassertions/pull/1760)
1719

1820
## 6.2.0
1921

0 commit comments

Comments
 (0)