{"record":{"id":"56f2ba14d9c5124d","repo":"devlooped/moq","slug":"type-0-does-not-implement-required-interface-1","errorCode":null,"errorMessage":"Type {0} does not implement required interface {1}","messagePattern":"Type (.+?) does not implement required interface (.+?)","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Moq/Guard.cs","lineNumber":41,"sourceCode":"            {\n                throw new ArgumentException(\n                    string.Format(\n                        CultureInfo.CurrentCulture,\n                        Resources.TypeHasNoDefaultConstructor,\n                        type.GetFormattedName()));\n            }\n        }\n\n        public static void ImplementsInterface(Type interfaceType, Type type, string? paramName = null)\n        {\n            Debug.Assert(interfaceType != null);\n            Debug.Assert(interfaceType.IsInterface);\n\n            Debug.Assert(type != null);\n\n            if (interfaceType.IsAssignableFrom(type) == false)\n            {\n                throw new ArgumentException(\n                    string.Format(\n                        CultureInfo.CurrentCulture,\n                        Resources.TypeNotImplementInterface,\n                        type.GetFormattedName(),\n                        interfaceType.GetFormattedName()),\n                    paramName);\n            }\n        }\n\n        public static void ImplementsTypeMatcherProtocol(Type type)\n        {\n            Debug.Assert(type != null);\n\n            Guard.ImplementsInterface(typeof(ITypeMatcher), type);\n            Guard.CanCreateInstance(type);\n        }\n\n        public static void IsAssignmentToPropertyOrIndexer(LambdaExpression expression, string paramName)","sourceCodeStart":23,"sourceCodeEnd":59,"githubUrl":"https://github.com/devlooped/moq/blob/89a5be629c752960fe403e95ff583e4ae6f00542/src/Moq/Guard.cs#L23-L59","documentation":"Guard.ImplementsInterface verifies that a given type actually implements the interface required by the operation before proceeding. When interfaceType.IsAssignableFrom(type) is false, Moq throws ArgumentException naming both the type and the missing interface. It prevents later invalid casts or mock misconfiguration.","triggerScenarios":"Registering or wiring a type where an interface is required — e.g. passing a service type to a mock/proxy factory, mock.As<TInterface> with an incompatible type, or generic APIs guarded by this check with a mismatched type argument.","commonSituations":"Refactoring renamed or removed an interface implementation; a DI/mock configuration registers a concrete type that no longer implements the declared interface; wrong generic type argument supplied to setup helpers.","solutions":["Make the type implement the required interface (add `: IRequired` and members).","Pass a type that actually implements the interface.","Fix the interface name/generic argument if you passed the wrong one.","Check for version mismatches where the interface moved to a different assembly/namespace."],"exampleFix":"// before\nGuard.ImplementsInstance(typeof(Service), paramName, typeof(IRepo)); // Service does not implement IRepo\nclass Service { }\n// after\nclass Service : IRepo { public Thing Get(int id) => ...; }","handlingStrategy":"type-guard","validationCode":"if (!typeof(IRequired).IsAssignableFrom(typeof(Service))) throw new InvalidOperationException($\"{typeof(Service).Name} must implement {nameof(IRequired)}\");","typeGuard":"bool Implements<TInterface>(Type t) where TInterface : class => typeof(TInterface).IsAssignableFrom(t);","tryCatchPattern":"try { Guard.ImplementsInstance(serviceType, nameof(serviceType), typeof(IRepo)); }\ncatch (ArgumentException ex) when (ex.Message.Contains(\"does not implement required interface\")) { /* fix the type or registration */ throw; }","preventionTips":["Add compile-time constraints (where T : IRepo) so mismatches fail at compile time.","Run a reflection-based startup test asserting service registrations implement their interfaces.","Re-check interface implementations after renaming/refactoring interfaces."],"tags":["moq","interface","type-check"],"backgroundTag":"type-mismatch","analyzedSha":"89a5be629c752960fe403e95ff583e4ae6f00542","analyzedAt":"2026-09-16T05:31:39.496Z","contentChangedAt":"2026-09-16T05:31:39.496Z","schemaVersion":2},"datasetVersion":"2026-09-16T09:17:16.951Z"}