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.

Include extension methods for `OccurrenceConstraint` that reads more fluently

See original GitHub issue

I 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:closed
  • Created 10 months ago
  • Comments:9 (9 by maintainers)

github_iconTop GitHub Comments

1reaction
jnyrupcommented, Dec 29, 2022

Then let’s go with TimesExactly().

Approved API

+ public static class OccurrenceConstraintExtensions
+  {
+      public static FluentAssertions.OccurrenceConstraint TimesExactly(this int times) { }
+      public static FluentAssertions.OccurrenceConstraint TimesOrLess(this int times) { }
+      public static FluentAssertions.OccurrenceConstraint TimesOrMore(this int times) { }
+  }
1reaction
IT-VBFKcommented, Dec 26, 2022

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

+ public static class OccurrenceConstraintExtensions
+  {
+      public static FluentAssertions.OccurrenceConstraint Times(this int times) { }
+      public static FluentAssertions.OccurrenceConstraint TimesOrLess(this int times) { }
+      public static FluentAssertions.OccurrenceConstraint TimesOrMore(this int times) { }
+  }
Read more comments on GitHub >

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

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