{"record":{"id":"3f6dd07f6baefb46","repo":"devlooped/moq","slug":"resources-asmustbeinterface","errorCode":null,"errorMessage":"Resources.AsMustBeInterface","messagePattern":"Resources\\.AsMustBeInterface","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Moq/Mock`1.cs","lineNumber":384,"sourceCode":"        ///   The following example creates a mock for the main interface\n        ///   and later adds <see cref=\"IDisposable\"/> to it to verify it's called by the consumer code:\n        ///   <code>\n        ///     var mock = new Mock&lt;IProcessor&gt;();\n        ///     mock.Setup(x =&gt; x.Execute(\"ping\"));\n        ///\n        ///     // add IDisposable interface\n        ///     var disposable = mock.As&lt;IDisposable&gt;();\n        ///     disposable.Setup(d => d.Dispose())\n        ///               .Verifiable();\n        ///   </code>\n        /// </example>\n        public override Mock<TInterface> As<TInterface>()\n        {\n            var interfaceType = typeof(TInterface);\n\n            if (!interfaceType.IsInterface)\n            {\n                throw new ArgumentException(Resources.AsMustBeInterface);\n            }\n\n            if (typeof(TInterface) == typeof(T))\n            {\n                return (Mock<TInterface>)(Mock)this;\n            }\n\n            if (this.IsObjectInitialized && this.ImplementsInterface(interfaceType) == false)\n            {\n                throw new InvalidOperationException(Resources.AlreadyInitialized);\n            }\n\n            if (this.AdditionalInterfaces.Contains(interfaceType) == false)\n            {\n                // We get here for either of two reasons:\n                //\n                // 1. We are being asked to implement an interface that the mocked type does *not* itself\n                //    inherit or implement. We need to hand this interface type to DynamicProxy's","sourceCodeStart":366,"sourceCodeEnd":402,"githubUrl":"https://github.com/devlooped/moq/blob/89a5be629c752960fe403e95ff583e4ae6f00542/src/Moq/Mock`1.cs#L366-L402","documentation":"Moq throws this ArgumentException when Mock<T>.As<TInterface>() is called with a TInterface that is not an interface. As<T>() exists to add additional interface implementations to a mock, so only interface type arguments are legal.","triggerScenarios":"mock.As<SomeClass>() or mock.As<SomeStruct>() — any call to As<TInterface> where typeof(TInterface).IsInterface is false (src/Moq/Mock`1.cs:384).","commonSituations":"Using As<T>() to cast to a base class (mistakenly thinking it performs a cast); generic code with an unconstrained T passed to As<T>.","solutions":["Only pass interface types to As<TInterface>.","To cast the mock itself, use (Mock<TDerived>)mock or As<TDerived> only when TDerived is an interface the type can implement.","Add a 'where TInterface : class' + runtime IsInterface check in generic wrappers before calling As."],"exampleFix":"// before\nvar m = mock.As<MyBaseClass>();\n// after\nvar m = mock.As<IDerivedInterface>();","handlingStrategy":"type-guard","validationCode":"if (!typeof(TInterface).IsInterface)\n    throw new ArgumentException(\"As<TInterface> requires an interface type.\");","typeGuard":"bool IsInterfaceArg<TInterface>() => typeof(TInterface).IsInterface;","tryCatchPattern":"try { return mock.As<TInterface>(); } catch (ArgumentException ex) when (ex.Message.Contains(\"interface\")) { throw new InvalidOperationException(\"Pass an interface type to As<T>.\"); }","preventionTips":["Only pass interfaces to As<T>()","In generic wrappers constrain or runtime-check TInterface.IsInterface","Use a plain cast for class types instead of As<T>"],"tags":["moq","as-interface","invalid-argument","type-mismatch"],"backgroundTag":"invalid-argument-value","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"}