TNG / ArchUnitNET

A C# architecture test library to specify and assert architecture rules in C# for automated testing.
Apache License 2.0
826 stars 55 forks source link

How to check if a type overrides certain methods? #206

Closed x789 closed 1 year ago

x789 commented 1 year ago

I like to use ArchUnit in a project where certain classes need to override the Equals and GetHashCode methods. Since these are virtual methods of object, the creation of an interface and a check for its implementation will not work.

I have tried the following:

Classes().Should().HaveMethodMemberWithName("Equals");

as well as

Classes().Should().HaveMemberWithName("Equals");

Does anyone have a hint on how I can implement this check?

x789 commented 1 year ago

solved

I thought about it again. .NET does not know the principle of overriding. The possibility to override is a C# thing.

Therefore the approach to see if there is a method with the name Equals was right, but the syntax was wrong. The parameters were missing.

The following code checks (successfully) if a type implements (and thus overwrites) the System.Equals(System.Object) method.

Classes().That().ResideInNamespace("Domain").Should().HaveMemberWithName("Equals(System.Object)").Check(architecture);