-
I have this method to assert that a record is valid:
How can I extend assertions to do something like this?:
|
Beta Was this translation helpful? Give feedback.
Answered by
thomhurst
Dec 21, 2024
Replies: 1 comment 4 replies
-
Create an extension like this: public static InvokableValueAssertionBuilder<RedisLockRecord> IsValid(this IValueSource<RedisLockRecord> valueSource, string resourceId, TimeSpan timeToLive, string lockedByDescription, [CallerArgumentExpression(nameof(resourceId))] string expression1 = "", [CallerArgumentExpression(nameof(timeToLive))] string expression2 = "", [CallerArgumentExpression(nameof(lockedByDescription))] string expression3 = "")
{
return valueSource.RegisterAssertion(new RedisLockRecordValidAssertCondition(resourceId, timeToLive, lockedByDescription), [expression1, expression2, expression3]);
} and create a class representing your assertion with the logic in it. e.g.: public class RedisLockRecordValidAssertCondition(string resourceId, TimeSpan timeToLive, string lockedByDescription)
: BaseAssertCondition<RedisLockRecord>
{
protected override string GetExpectation()
{
return $"is valid with resourceId of {resourceId}, timeToLive of {timeToLive} and lockedByDescription of {lockedByDescription}";
}
protected override Task<AssertionResult> GetResult(RedisLockRecord? actualValue, Exception exception)
{
return AssertionResult.FailIf(() => actualValue is null, "RedisLockRecord is null")
.OrFailIf(() => actualValue.ResourceId != resourceId, "Resource id mismatch")
.OrFailIf(() => actualValue.LockId == Guid.Empty);
// etc
}
} |
Beta Was this translation helpful? Give feedback.
4 replies
Answer selected by
jesuslpm
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Create an extension like this:
and create a class representing your assertion with the logic in it.
e.g.: