-
Notifications
You must be signed in to change notification settings - Fork 729
Implemented BeSingle and HaveCount for XElement #1681 #1684
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What we've added for XDocument
should also be added to XElement
.
Thanks for the feedback. Hopefully I understood them correctly. Now everything should be fine. |
/// <param name="becauseArgs"> | ||
/// Zero or more objects to format using the placeholders in <paramref name="because" />. | ||
/// </param> | ||
public AndConstraint<XDocumentAssertions> HaveSingleElement(XName expected, string because = "", params object[] becauseArgs) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should return a AndWhichConstraint<XDocumentAssertions, XElement>
like HaveElement
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And use the Which
that AndWhichConstraint
exposes in your test
} | ||
|
||
Guard.ThrowIfArgumentIsNull(expected, nameof(expected), | ||
"Cannot assert the document has an element count if the element name is <null>*"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should the trailing *
be a .
?
"Expected {context:subject} to have root element with child {0}{reason}, but it has no root element.", | ||
expected.ToString()); | ||
|
||
var xElements = Subject.Root.Elements(expected); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If HaveElementCount
is called inside an AssertionScope
the assertion above above will not fail immediately, so Subject.Root
can be null here.
See e.g. XElementAssertions.HaveValue
for an example of how to handle this.
</parent>"); | ||
|
||
// Act | ||
Action act = () => document.Should().HaveSingleElement("child"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You could also validate that because
and becauseArgs
are properly passed along in this test.
See e.g. XDocumentAssertionSpecs.When_the_expected_element_is_null_it_fails
* Adding new overloads to all `GreaterOrEqualTo` and `LessOrEqualTo` assertions, adding the word `Than` - [#1673](https://github.com/fluentassertions/fluentassertions/pull/1673) | ||
* `BeAsync()` and `NotBeAsync()` are now also available on `MethodInfoSelectorAssertions` - [#1700](https://github.com/fluentassertions/fluentassertions/pull/1700) | ||
|
||
* Added `HaveSingleElement` and `HaveElementCount` for `XDocument` - [#1681](https://github.com/fluentassertions/fluentassertions/pull/1684) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
* Added `HaveSingleElement` and `HaveElementCount` for `XDocument` - [#1681](https://github.com/fluentassertions/fluentassertions/pull/1684) | |
* Added `HaveSingleElement` and `HaveElementCount` for `XDocument` - [#1684](https://github.com/fluentassertions/fluentassertions/pull/1684) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I suggested some improvements to the test names to avoid violating rule 4 of https://www.continuousimprover.com/2021/10/laws-test-driven-development.html
/// <param name="becauseArgs"> | ||
/// Zero or more objects to format using the placeholders in <paramref name="because" />. | ||
/// </param> | ||
public AndConstraint<XDocumentAssertions> HaveSingleElement(XName expected, string because = "", params object[] becauseArgs) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And use the Which
that AndWhichConstraint
exposes in your test
} | ||
|
||
/// <summary> | ||
/// Asserts that the <see cref="XDocument.Root"/> element of the current <see cref="XDocument"/> has a the specified <paramref name="count"/> of |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's a typo around "has a the specified"
public AndConstraint<XDocumentAssertions> HaveElementCount(XName expected, int count, string because = "", | ||
params object[] becauseArgs) | ||
{ | ||
if (Subject is null) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The subject being null
is actually something that could happen in a failing test, so I think you should use the normal ForCondition
logic.
#region HaveSingleElement | ||
|
||
[Fact] | ||
public void When_asserting_document_has_a_single_child_element_and_it_does_it_should_succeed() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🔧 I suggest to keep the name more functional and concise: A_single_expected_child_element_with_the_specified_name_should_be_accepted
} | ||
|
||
[Fact] | ||
public void When_asserting_document_has_single_child_element_but_it_does_have_two_it_should_fail() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🔧 Suggestion: Multiple_child_elements_with_the_specified_name_should_fail_the_test
} | ||
|
||
[Fact] | ||
public void When_asserting_a_null_xDocument_to_have_a_single_element_it_should_fail() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🔧 Suggestion: Cannot_ensure_a_single_element_if_the_document_is_null
@ABergBN do you still intend to finish this PR? |
IMPORTANT
AcceptApiChanges.ps1
/AcceptApiChanges.sh
.Implemented the two needed methods for checking the counting on XElement (#1681)