Make it possible to chain together asserts on a subject.
See original GitHub issueDescription
Feature request: Make it possible to chain together asserts on a subject.
This will have the following benefits
- more fluent typing. Typing “.A” can autocomplete “Also”, while typing “;” will not autocomplete “result.” Which is needed for the next test case
- Better group relevant assertions
- less technical, and supports stream of consciousness dapping down the asserts and reading them
Complete minimal example reproducing the issue
Usually we assert we use the same variable result
multiple times
E.g.
var result = a.Parts.GetStuff(parameters);
result.Code.Should().Be(1);
result.Explanation.Should().Be("");
result.Ids.Should().HaveCount(1);
I was wondering if this is easier to read and type
var result = a.Parts.GetStuff(parameters);
result
.With.Code.Should().Be(1)
.Also.Explanation.Should().Be("")
.Also.Ids.Should().HaveCount(1);
I guess the With
is needed to bind what Also
refers back to, similar to how And
refers back to the last subject
It also allows better separation of logical grouping
var result = a.Parts.GetStuff(parameters);
result
.With.Code.Should().Be(1)
.Also.Explanation.Should().Be("")
.Also.Ids.Should().HaveCount(1);
result
.With.Amount.Should().Be(400)
.Also.AmountDetails.Should().Be("usd");
I prefer this syntax over
var result = a.Parts.GetStuff(parameters);
result.Should().BeEquivalientTo(new {
Code = 1,
Explanation = "",
Ids = new []{ ?? }
});
since it has autocompletion and static typing
Issue Analytics
- State:
- Created 3 years ago
- Comments:5 (4 by maintainers)
Top Results From Across the Web
Are multiple asserts bad in a unit test? Even if chaining?
Use of multiple asserts is OK if they are testing the same thing. For example, it's OK to do: Assert.IsNotNull(value); Assert.AreEqual(0, value ...
Read more >Grouped Assertions In JUnit 5 - Tutorial With Examples
JUnit 5 supports an additional feature called Grouped assertions to execute multiple assertions together and get one consolidated report.
Read more >Is it OK to have multiple asserts in a single unit test?
Yes, it is ok to have multiple assertions as long as a failing test gives you enough information to be able to diagnose...
Read more >should | Cypress Documentation
Multiple Assertions Cypress makes it easier to chain assertions together. In this example we use .and() which is identical to .should() .
Read more >assertAll() vs Multiple Assertions in JUnit5
However, we can group assertions together, and report on their combined success or failure, using assertAll() within JUnit 5.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
This is simply not possible.
A way to write it completely fluent is with the
Match
assertionIf you want to have the result of all three assertions, in case the first one fails, you can wrap them in an
AssertionScope
.While I do like the idea of an even more fluent API I can’t see how this is technically possible. I’ll close this issue for now, but feel free to re-open if you have new light to shed on this.