-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: Add tests to ensure Command/Queries always returns a Result (#6)
* chore: Add tests to ensure Command/Queries always returns a Result * nit
- Loading branch information
Showing
2 changed files
with
82 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
using FluentResults; | ||
using MediatR; | ||
using Mono.Cecil; | ||
using NetArchTest.Rules; | ||
|
||
namespace Core.Tests.Rules; | ||
|
||
public class RequestShouldWrapResultRule : ICustomRule | ||
{ | ||
private static readonly Type ResultType = typeof(Result); | ||
private static readonly Type ResultGenericType = typeof(Result<>); | ||
private static readonly Type RequestGenericType = typeof(IRequest<>); | ||
|
||
public bool MeetsRule(TypeDefinition typeDefinition) | ||
{ | ||
// Should be a single interface that is IRequest<T> | ||
var requestType = typeDefinition.Interfaces | ||
.Single(i => | ||
TypeReferenceMatchesType(i.InterfaceType, RequestGenericType)) | ||
.InterfaceType as GenericInstanceType; | ||
|
||
var responseType = requestType.GenericArguments[0]; | ||
|
||
// Response type should be Result or Result<T> | ||
return TypeReferenceMatchesType(responseType, ResultType) | ||
|| TypeReferenceMatchesType(responseType, ResultGenericType); | ||
} | ||
|
||
private static bool TypeReferenceMatchesType(TypeReference typeReference, Type type) | ||
{ | ||
return typeReference.Name == type.Name && typeReference.Namespace == type.Namespace; | ||
} | ||
} |