{"record":{"id":"a50c927212093fab","repo":"devlooped/moq","slug":"resources-argumentmatcherwillnevermatch-formatted-with","errorCode":null,"errorMessage":"Resources.ArgumentMatcherWillNeverMatch (formatted with matcher expression, operand type, parameter type)","messagePattern":"Resources\\.ArgumentMatcherWillNeverMatch \\(formatted with matcher expression, operand type, parameter type\\)","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Moq/MatcherFactory.cs","lineNumber":115,"sourceCode":"                {\n                    if (convertExpression.Operand.IsMatch(out var match))\n                    {\n                        Type matchedValuesType;\n\n                        if (match.GetType().IsGenericType)\n                        {\n                            // If match type is `Match<int>`, matchedValuesType set to `int`\n                            // Fix for https://github.com/moq/moq4/issues/1199\n                            matchedValuesType = match.GetType().GenericTypeArguments[0];\n                        }\n                        else\n                        {\n                            matchedValuesType = convertExpression.Operand.Type;\n                        }\n\n                        if (!matchedValuesType.IsAssignableFrom(parameter.ParameterType))\n                        {\n                            throw new ArgumentException(\n                                string.Format(\n                                    Resources.ArgumentMatcherWillNeverMatch,\n                                    convertExpression.Operand.ToStringFixed(),\n                                    convertExpression.Operand.Type.GetFormattedName(),\n                                    parameter.ParameterType.GetFormattedName()));\n                        }\n                    }\n                }\n            }\n\n            return MatcherFactory.CreateMatcher(argument);\n        }\n\n        public static Pair<IMatcher, Expression> CreateMatcher(Expression expression)\n        {\n            // Type inference on the call might \n            // do automatic conversion to the desired \n            // method argument type, and a Convert expression type ","sourceCodeStart":97,"sourceCodeEnd":133,"githubUrl":"https://github.com/devlooped/moq/blob/89a5be629c752960fe403e95ff583e4ae6f00542/src/Moq/MatcherFactory.cs#L97-L133","documentation":"Moq throws this ArgumentException during matcher creation when the matcher's matched-value type cannot be assigned to the mocked method's parameter type — i.e. the argument matcher will never match. It compares the operand type of a Convert expression against the parameter's type using IsAssignableFrom.","triggerScenarios":"Setting up a call with an argument matcher (It.Is<T>, It.IsAny<T>) whose T differs incompatibly from the parameter type, e.g. `mock.Setup(m => m.Method(It.Is<int>(x => x > 5)))` on a method taking a string, or a conversion the parameter type cannot accept.","commonSituations":"Type mismatches after method signature changes/refactoring; using the wrong generic type argument to It.Is; overload resolution picking a different parameter type than expected.","solutions":["Change the matcher's generic type so it matches the method parameter type exactly","Verify the overload being mocked and its parameter types","If a conversion is intended, explicitly convert the matched value (e.g. It.Is<int>(...) on an object parameter won't work — match the declared type)"],"exampleFix":"// before\nmock.Setup(m => m.Save(It.Is<int>(id => id > 0))); // Save takes string\n// after\nmock.Setup(m => m.Save(It.Is<string>(s => int.Parse(s) > 0)));","handlingStrategy":"validation","validationCode":"var paramType = typeof(IFoo).GetMethod(nameof(IFoo.Save))!.GetParameters()[0].ParameterType;\nif (!paramType.IsAssignableFrom(typeof(int))) throw new InvalidOperationException(\"Matcher type must be assignable to parameter type\");","typeGuard":"static bool MatcherMatchesParam<TParam>(Expression<Func<TParam, bool>> _) => typeof(TParam) == typeof(TParam); // align T with the method's declared parameter type","tryCatchPattern":"try { mock.Setup(m => m.Save(It.Is<int>(x => x > 0))); } catch (ArgumentException ex) when (ex.Message.Contains(\"never match\")) { /* fix matcher generic type */ }","preventionTips":["Match It.Is<T>'s T to the parameter's declared type","Re-run tests after signature refactors","Prefer typed setup helpers over raw It.Is expressions"],"tags":["moq","argument-matcher","type-mismatch","setup"],"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"}