question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Make it possible to chain together asserts on a subject.

See original GitHub issue

Description

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:closed
  • Created 3 years ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
jnyrupcommented, Feb 4, 2021

This is simply not possible.

var result = a.Parts.GetStuff(parameters);
result <-- we have an object of type Stuff at hand
    .With.Code.Should().Be(1) <--- we wrap it into NumericAssertions<int>
           ^-------- we're now have an int
    .Also.Explanation.Should().Be("")
            ^-- this would need to know that the int NumericAssertions<int> is wrapping, is a property on the Stuff type.

A way to write it completely fluent is with the Match assertion

result.Should()
    .Match<Stuff>(e => e.Code == 1)
    .And.Match<Stuff>(e => e.Explanation == "")
    .And.Match<Stuff>(e => e.Ids.Count == 1);

If you want to have the result of all three assertions, in case the first one fails, you can wrap them in an AssertionScope.

using(new AssertionScope())
{
	result
    	.With.Code.Should().Be(1)
	    .Also.Explanation.Should().Be("")
    	.Also.Ids.Should().HaveCount(1);
}
0reactions
jnyrupcommented, Feb 7, 2021

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found