{"record":{"id":"7539144f3a78ca30","repo":"devlooped/moq","slug":"it-is-impossible-to-call-the-provided-strongly-typed","errorCode":null,"errorMessage":"It is impossible to call the provided strongly-typed predicate due to the use of a type matcher. Provide a weakly-typed predicate with two parameters (object, Type) instead.","messagePattern":"It is impossible to call the provided strongly-typed predicate due to the use of a type matcher\\. Provide a weakly-typed predicate with two parameters \\(object, Type\\) instead\\.","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Moq/It.cs","lineNumber":143,"sourceCode":"        ///   the <c>Do</c> method is an even number.\n        ///   <code>\n        ///     mock.Setup(x =&gt; x.Do(It.Is&lt;int&gt;(i =&gt; i % 2 == 0)))\n        ///         .Returns(1);\n        ///   </code>\n        /// </example>\n        /// <example>\n        ///   This example shows how to throw an exception if the argument to the method\n        ///   is a negative number:\n        ///   <code>\n        ///     mock.Setup(x =&gt; x.GetUser(It.Is&lt;int&gt;(i =&gt; i &lt; 0)))\n        ///         .Throws(new ArgumentException());\n        ///   </code>\n        /// </example>\n        public static TValue Is<TValue>(Expression<Func<TValue, bool>> match)\n        {\n            if (typeof(TValue).IsOrContainsTypeMatcher())\n            {\n                throw new ArgumentException(Resources.UseItIsOtherOverload, nameof(match));\n            }\n\n            var thisMethod = (MethodInfo)MethodBase.GetCurrentMethod()!;\n            var compiledMatchMethod = match.CompileUsingExpressionCompiler();\n\n            return Match.Create<TValue>(\n                argument => compiledMatchMethod.Invoke(argument),\n                Expression.Lambda<Func<TValue>>(Expression.Call(thisMethod.MakeGenericMethod(typeof(TValue)), match)));\n        }\n\n        /// <summary>\n        ///   Matches any value that satisfies the given predicate.\n        ///   <para>\n        ///     Use this overload when you specify a type matcher for <typeparamref name=\"TValue\"/>.\n        ///     The <paramref name=\"match\"/> callback you provide will then receive the actual parameter type\n        ///     as well as the invocation argument.\n        ///   </para>\n        /// </summary>","sourceCodeStart":125,"sourceCodeEnd":161,"githubUrl":"https://github.com/devlooped/moq/blob/89a5be629c752960fe403e95ff583e4ae6f00542/src/Moq/It.cs#L125-L161","documentation":"It.Is<TValue>(Expression<Func<TValue,bool>>) builds a matcher from a strongly-typed predicate. If TValue itself is or contains a type matcher (e.g. It.IsAnyType), the compiled predicate cannot be invoked safely, so Moq throws ArgumentException before creating the match. Moq requires the weakly-typed It.Is overload taking an (object, Type) predicate when type matchers are involved.","triggerScenarios":"It.Is<It.IsAnyType>(x => ...) or It.Is<object> containing It.IsAnyType nested in the value type — the Is<TValue> method checks typeof(TValue).IsOrContainsTypeMatcher() and throws ArgumentException naming the 'match' parameter.","commonSituations":"Trying to match generic method arguments or 'any type' parameters (e.g. verifying a call with a generic parameter) using the strongly-typed It.Is overload instead of the (object, Type) one; copy-pasting normal matcher code to a generic verification.","solutions":["Use the two-parameter weakly-typed overload: It.Is((object v, Type t) => ...) instead of It.Is<TValue>(x => ...).","If you know the concrete type, use It.Is<ConcreteType>(x => ...) instead of It.IsAnyType.","Inside the weakly-typed predicate, cast the object argument to the expected runtime type before asserting."],"exampleFix":"// before\nmock.Verify(m => m.Send(It.Is<It.IsAnyType>(x => x.ToString() == \"hi\")));\n// after\nmock.Verify(m => m.Send(It.Is((object x, Type t) => x.ToString() == \"hi\")));","handlingStrategy":"type-guard","validationCode":"if (typeof(TValue) == typeof(It.IsAnyType) || typeof(TValue).ContainsGenericParameters)\n    useWeaklyTypedOverload = true; // use It.Is((object v, Type t) => ...) instead","typeGuard":"static bool UsesTypeMatcher<TValue>() => typeof(TValue).IsOrContainsTypeMatcher(); // then choose overload accordingly","tryCatchPattern":"try { expr = It.Is<TValue>(predicate); }\ncatch (ArgumentException) { expr = It.Is((object v, Type t) => predicate((TValue)v)); }","preventionTips":["Whenever verifying generic/It.IsAnyType arguments, immediately reach for the (object, Type) It.Is overload.","Cast inside weakly-typed predicates rather than parameterizing matchers with It.IsAnyType.","Use the concrete type in It.Is<T> when it is known at compile time."],"tags":["moq","argument-matcher","linq-expressions","generics","csharp"],"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"}