Class ResultAssertions<TValue>
Contains assertion methods for Result types.
public class ResultAssertions<TValue> : ReferenceTypeAssertions<Result<TValue>, ResultAssertions<TValue>>
Type Parameters
TValue
- Inheritance
-
ResultAssertions<TValue>
- Inherited Members
- Extension Methods
Constructors
ResultAssertions(Result<TValue>)
Initializes a new instance of the ResultAssertions<TValue> class.
public ResultAssertions(Result<TValue> result)
Parameters
resultResult<TValue>
Properties
Identifier
Returns the type of the subject the assertion applies on. It should be a user-friendly name as it is included in the failure message.
protected override string Identifier { get; }
Property Value
Methods
Be(Result<TValue>, string, params object[])
Asserts that the result is equal to the expected result (using Equals(Result<TValue>)).
public AndConstraint<ResultAssertions<TValue>> Be(Result<TValue> expected, string because = "", params object[] becauseArgs)
Parameters
Returns
- AndConstraint<ResultAssertions<TValue>>
BeFailure(string, params object[])
Asserts that the result is a failure.
public AndWhichConstraint<ResultAssertions<TValue>, Error> BeFailure(string because = "", params object[] becauseArgs)
Parameters
becausestringA formatted phrase explaining why the assertion is needed.
becauseArgsobject[]Zero or more objects to format using the placeholders in
because.
Returns
- AndWhichConstraint<ResultAssertions<TValue>, Error>
BeFailureOfType<TError>(string, params object[])
Asserts that the result is a failure with a specific error type.
public AndWhichConstraint<ResultAssertions<TValue>, TError> BeFailureOfType<TError>(string because = "", params object[] becauseArgs) where TError : Error
Parameters
becausestringA formatted phrase explaining why the assertion is needed.
becauseArgsobject[]Zero or more objects to format using the placeholders in
because.
Returns
- AndWhichConstraint<ResultAssertions<TValue>, TError>
Type Parameters
TErrorThe expected error type.
Remarks
Behavior under AssertionScope. Without
an active scope, a wrong-type assertion throws via FailWith and execution stops
immediately. With an active scope, the failure is recorded and execution continues —
the returned AndWhichConstraint.Which is default (null for
reference types). Chaining .Which.Foo.Should()... after a wrong-type
BeFailureOfType<TError>() would normally throw
NullReferenceException on the null typed error; in practice the
surrounding using var scope = new AssertionScope() disposes via the language
finally block and raises the recorded assertion failure, which masks the chained NRE.
Net effect: the test still sees the "expected TError, found ..."
assertion message. (The chained-.Which pattern is still considered an anti-pattern
inside scopes — assert the type in its own statement and access .Which only after
you know the type matches.)
BeSuccess(string, params object[])
Asserts that the result is a success.
public AndWhichConstraint<ResultAssertions<TValue>, TValue> BeSuccess(string because = "", params object[] becauseArgs)
Parameters
becausestringA formatted phrase as is supported by Format(string, params object[]) explaining why the assertion is needed. If the phrase does not start with the word because, it is prepended automatically.
becauseArgsobject[]Zero or more objects to format using the placeholders in
because.
Returns
- AndWhichConstraint<ResultAssertions<TValue>, TValue>
HaveErrorCode(string, string, params object[])
Asserts that the failure has a specific error code.
public AndConstraint<ResultAssertions<TValue>> HaveErrorCode(string expectedCode, string because = "", params object[] becauseArgs)
Parameters
expectedCodestringThe expected error code.
becausestringA formatted phrase explaining why the assertion is needed.
becauseArgsobject[]Zero or more objects to format using the placeholders in
because.
Returns
- AndConstraint<ResultAssertions<TValue>>
HaveErrorDetail(string, string, params object[])
Asserts that the failure has a specific error detail.
public AndConstraint<ResultAssertions<TValue>> HaveErrorDetail(string expectedDetail, string because = "", params object[] becauseArgs)
Parameters
expectedDetailstringThe expected error detail.
becausestringA formatted phrase explaining why the assertion is needed.
becauseArgsobject[]Zero or more objects to format using the placeholders in
because.
Returns
- AndConstraint<ResultAssertions<TValue>>
HaveErrorDetailContaining(string, string, params object[])
Asserts that the failure error detail contains the specified substring.
public AndConstraint<ResultAssertions<TValue>> HaveErrorDetailContaining(string substring, string because = "", params object[] becauseArgs)
Parameters
substringstringThe substring to search for.
becausestringA formatted phrase explaining why the assertion is needed.
becauseArgsobject[]Zero or more objects to format using the placeholders in
because.
Returns
- AndConstraint<ResultAssertions<TValue>>
HaveValue(TValue, string, params object[])
Asserts that the success value equals the expected value.
public AndConstraint<ResultAssertions<TValue>> HaveValue(TValue expectedValue, string because = "", params object[] becauseArgs)
Parameters
expectedValueTValueThe expected value.
becausestringA formatted phrase explaining why the assertion is needed.
becauseArgsobject[]Zero or more objects to format using the placeholders in
because.
Returns
- AndConstraint<ResultAssertions<TValue>>
HaveValueEquivalentTo(TValue, string, params object[])
Asserts that the success value is equivalent to the expected value using structural comparison.
public AndConstraint<ResultAssertions<TValue>> HaveValueEquivalentTo(TValue expectedValue, string because = "", params object[] becauseArgs)
Parameters
expectedValueTValueThe expected value.
becausestringA formatted phrase explaining why the assertion is needed.
becauseArgsobject[]Zero or more objects to format using the placeholders in
because.
Returns
- AndConstraint<ResultAssertions<TValue>>
HaveValueMatching(Func<TValue, bool>, string, params object[])
Asserts that the success value satisfies a predicate.
public AndConstraint<ResultAssertions<TValue>> HaveValueMatching(Func<TValue, bool> predicate, string because = "", params object[] becauseArgs)
Parameters
predicateFunc<TValue, bool>The predicate the value should satisfy.
becausestringA formatted phrase explaining why the assertion is needed.
becauseArgsobject[]Zero or more objects to format using the placeholders in
because.
Returns
- AndConstraint<ResultAssertions<TValue>>
NotBe(Result<TValue>, string, params object[])
Asserts that the result is not equal to the unexpected result.
public AndConstraint<ResultAssertions<TValue>> NotBe(Result<TValue> unexpected, string because = "", params object[] becauseArgs)
Parameters
Returns
- AndConstraint<ResultAssertions<TValue>>