Open up `OccurenceConstraint` for usage in custom assertion extensions
See original GitHub issueThe OccurrenceConstraint.Assert()
method is internal - so people who write their own extensions cannot use this very handy constraint (and therefore create their own - creating inconsistency of how to deal with assertion occurences).
Understand why this is internal - so the actual usage hides this when defining an assertion for the test.
However why not add a new overload in the Execute
assertion fluent API to accept a OccurrenceConstraint
. i.e. as an alternative to ForCondition(...)
:
Execute.Assertion
.BecauseOf(because, becauseArgs)
.ForConstraint(constraint)
.FailWith("Expected {0}{reason} but found {1}",
constraint, Subject.Count());
Note there would be a formatter for the occurrence constraint to render the expected amount / “mode” (since these are also internal - and should continue to be)
Issue Analytics
- State:
- Created 3 years ago
- Comments:14 (9 by maintainers)
Top Results From Across the Web
Extensibility - 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 >Creating Custom Fluent Assertions | NimblePros Blog
The first step to creating a custom assertion is to define a class that inherits from ReferenceTypeAssertions<TSubject, TAssertions> abstract ...
Read more >How do I write CustomAssertion using FluentAssertions?
I managed to write my own extension method via syntax BeWorking(this ObjectAssertions obj, string because="", params object[] becauseArgs) in ...
Read more >Custom Assertions with AssertJ
In this tutorial, we'll walk through creating custom AssertJ assertions; the AssertJ's basics can be found here.
Read more >The definitive guide to extending Fluent Assertions
So in this case, our nicely created ContainFile extension method will display the directory that it used to assert that file existed. You...
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
After some research I can offer a few solutions for this feature. I’ll list and describe them below. @brooknovak @dennisdoomen @jnyrup It would be great if you could look through and give some comments on this.
A) Literally what @brooknovak asked
Overview:
ForConstraint
method is added specifically forOccurrenceConstraint
. A new formatter forOccurrenceConstraint
is also registered.Pros:
Cons:
ForConstraint
andFailWith
).ForConstraint
will have to be rewritten or more overloads added. b) The need to add a new formatter for new “assertable” constraint is not explicit.Possible implementation
Usage
B) Generalized solution
Overview:
ForConstraint
is added forAssertableConstraint
, which is an abstract base class for constraints withAssert
method. Probably, interface would be more suitable here, but it won’t allow keeping itinternal
. A new formatter forAssertableConstraint
is registered, which invokes formatting logic, defined inside the constraint implementation.Pros:
Cons:
Possible implementation
Usage
C) Generalized solution with placeholders for formatting
Overview: The same as B), but API is changed to use special placeholders
FailWith
message argument so there is no need for formatters.Pros:
Cons:
Possible implementation
Usage
Go for it @dennisdoomen - use whatever you like. I love this library, it’s really helped shape the way I approach testing!