public void Occurrence_constraint_passes(OccurrenceConstraint constraint, int occurrences) { // Act / Assert Execute.Assertion .ForConstraint(constraint, occurrences) .FailWith(""); }
public void Occurrence_constraint_fails(OccurrenceConstraint constraint, int occurrences) { // Act Action act = () => Execute.Assertion .ForConstraint(constraint, occurrences) .FailWith($"Expected occurrence to be {constraint.Mode} {constraint.ExpectedCount}, but it was {occurrences}"); // Assert act.Should().Throw <XunitException>() .WithMessage("Expected occurrence to be *, but it was *"); }
/// <summary> /// Asserts that the <see cref="XElement"/> of the current <see cref="XElement"/> has the specified occurrence of /// child elements with the specified <paramref name="expected"/> name. /// </summary> /// <param name="expected"> /// The name of the expected child element of the current element's <see cref="XElement"/>. /// </param> /// <param name="occurrenceConstraint"> /// A constraint specifying the number of times the specified elements should appear. /// </param> /// <param name="because"> /// A formatted phrase as is supported by <see cref="string.Format(string,object[])" /> explaining why the assertion /// is needed. If the phrase does not start with the word <i>because</i>, it is prepended automatically. /// </param> /// <param name="becauseArgs"> /// Zero or more objects to format using the placeholders in <paramref name="because" />. /// </param> public AndWhichConstraint <XElementAssertions, IEnumerable <XElement> > HaveElement(string expected, OccurrenceConstraint occurrenceConstraint, string because = "", params object[] becauseArgs) { Guard.ThrowIfArgumentIsNull(expected, nameof(expected), "Cannot assert the element has an element if the expected name is <null>."); return(HaveElement(XNamespace.None + expected, occurrenceConstraint, because, becauseArgs)); }
/// <summary> /// Asserts that the <see cref="XElement"/> of the current <see cref="XElement"/> has the specified occurrence of /// child elements with the specified <paramref name="expected"/> name. /// </summary> /// <param name="expected"> /// The full name <see cref="XName"/> of the expected child element of the current element's <see cref="XElement"/>. /// </param> /// <param name="occurrenceConstraint"> /// A constraint specifying the number of times the specified elements should appear. /// </param> /// <param name="because"> /// A formatted phrase as is supported by <see cref="string.Format(string,object[])" /> explaining why the assertion /// is needed. If the phrase does not start with the word <i>because</i>, it is prepended automatically. /// </param> /// <param name="becauseArgs"> /// Zero or more objects to format using the placeholders in <paramref name="because" />. /// </param> public AndWhichConstraint <XElementAssertions, IEnumerable <XElement> > HaveElement(XName expected, OccurrenceConstraint occurrenceConstraint, string because = "", params object[] becauseArgs) { Guard.ThrowIfArgumentIsNull(expected, nameof(expected), "Cannot assert the element has an element count if the element name is <null>."); bool success = Execute.Assertion .ForCondition(Subject is not null) .BecauseOf(because, becauseArgs) .FailWith( "Expected {context:subject} to have an element with count of {0}{reason}, but the element itself is <null>.", expected.ToString()); IEnumerable <XElement> xElements = Enumerable.Empty <XElement>(); if (success) { xElements = Subject.Elements(expected); int actual = xElements.Count(); Execute.Assertion .ForConstraint(occurrenceConstraint, actual) .BecauseOf(because, becauseArgs) .FailWith( $"Expected {{context:subject}} to have an element {{0}} {{expectedOccurrence}}" + $"{{reason}}, but found it {actual.Times()}.", expected.ToString()); } return(new AndWhichConstraint <XElementAssertions, IEnumerable <XElement> >(this, xElements)); }