Include extension methods for `OccurrenceConstraint` that reads more fluently
See original GitHub issueI was wondering if it would make sense to include an extension method for Exactly.Times
like this:
myString.Should().Contain("something", 4.Times()); // Equivalent to Exactly.Times(4)
I think it reads a bit better than the existing Exactly.Times(4)
, but perhaps this has already been considered?
I understand there might be some confusion around AtLeast
, AtMost
etc., but that might be fixed like:
myString.Should().Contain("something", 4.TimesOrMore()); // Equivalent to AtLeast.Times(4) and MoreThan(3)
myString.Should().Contain("something", 4.TimesOrLess()); // Equivalent to AtMost.Times(4) and LessThan(5)
There seems to be some redundancy in with AtLeast
/MoreThan
and AtMost
/LessThan
, but they might be useful in scenarios I’ve never had.
I know creating such extensions is pretty straightforward:
public static class FluentOccurrenceConstraintExtensions
{
public static OccurrenceConstraint Times(this int times)
{
return Exactly.Times(times);
}
public static OccurrenceConstraint TimesOrLess(this int times)
{
return AtLeast.Times(times);
}
public static OccurrenceConstraint TimesOrMore(this int times)
{
return AtMost.Times(times);
}
}
_Originally posted by @siewers in https://github.com/fluentassertions/fluentassertions/discussions/2001_
Issue Analytics
- State:
- Created 10 months ago
- Comments:9 (9 by maintainers)
Top Results From Across the Web
Open up `OccurenceConstraint` for usage in custom ...
The OccurrenceConstraint.Assert() method is internal - so people who write their own extensions cannot use this very handy constraint (and ...
Read more >c# - Calling Excluding method in extension ...
I have started using FluentAssertions library in my integration tests for the REST endpoints. The problem is I have to compare two entities,...
Read more >Using Extension Methods in C# to Build Fluent Code
In this article I'll attempt to demystify extension methods and illustrate how they can be used to build elegant and fluent code.
Read more >Fluent Assertions 6.0, the biggest release ever
Added ForConstraint method to AssertionScope that allows you to use an OccurrenceConstraint in your custom assertion extensions that can verify ...
Read more >Introduction - Fluent Assertions
A very extensive set of extension methods that allow you to more naturally specify the expected outcome of a TDD or BDD-style unit...
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
Then let’s go with
TimesExactly()
.Approved API
Ok… I thought the details were already discussed and kind of agreed on in #2001.
If not: here are the three methods I would add