{"record":{"id":"584ae93c5e9aa923","repo":"devlooped/moq","slug":"invalidoperationexception-unhandled-times-kind-in","errorCode":null,"errorMessage":"InvalidOperationException (unhandled Times kind in GetExceptionMessage)","messagePattern":"InvalidOperationException \\(unhandled Times kind in GetExceptionMessage\\)","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Moq/Times.cs","lineNumber":280,"sourceCode":"\n            if (this.kind == Kind.BetweenExclusive)\n            {\n                --from;\n                ++to;\n            }\n\n            var message = this.kind switch\n            {\n                Kind.AtLeastOnce => Resources.NoMatchingCallsAtLeastOnce,\n                Kind.AtLeast => Resources.NoMatchingCallsAtLeast,\n                Kind.AtMost => Resources.NoMatchingCallsAtMost,\n                Kind.AtMostOnce => Resources.NoMatchingCallsAtMostOnce,\n                Kind.BetweenExclusive => Resources.NoMatchingCallsBetweenExclusive,\n                Kind.BetweenInclusive => Resources.NoMatchingCallsBetweenInclusive,\n                Kind.Exactly => Resources.NoMatchingCallsExactly,\n                Kind.Once => Resources.NoMatchingCallsOnce,\n                Kind.Never => Resources.NoMatchingCallsNever,\n                _ => throw new InvalidOperationException(),\n            };\n\n            return string.Format(CultureInfo.CurrentCulture, message, from, to, callCount);\n        }\n\n        /// <summary>\n        ///   Checks whether the specified number of invocations matches the constraint described by this instance.\n        /// </summary>\n        /// <param name=\"count\">The number of invocations to check.</param>\n        /// <returns>\n        ///   <see langword=\"true\"/> if <paramref name=\"count\"/> matches the constraint described by this instance;\n        ///   otherwise, <see langword=\"false\"/>.\n        /// </returns>\n        public bool Validate(int count)\n        {\n            var (from, to) = this;\n            return from <= count && count <= to;\n        }","sourceCodeStart":262,"sourceCodeEnd":298,"githubUrl":"https://github.com/devlooped/moq/blob/89a5be629c752960fe403e95ff583e4ae6f00542/src/Moq/Times.cs#L262-L298","documentation":"Times.GetExceptionMessage(callCount) picks a resource string by Kind and throws InvalidOperationException when the kind is unrecognized. Like the ToString guard, it signals a Times value carrying an invalid Kind at the moment Moq formats a 'no matching calls' verification failure message.","triggerScenarios":"A Times instance with an out-of-range Kind reaches verification-failure message formatting — via invalid construction, deserialization, or mixed Moq assembly versions.","commonSituations":"Same as the ToString invariant break: hand-built Times values, assembly version skew in the test runner, corrupted state.","solutions":["Use only the public Times factories to create instances","Unify Moq package versions across the solution to eliminate enum/value skew","If unavoidable, wrap Verify calls in try-catch for InvalidOperationException to produce a diagnosable failure, and report the Moq bug"],"exampleFix":"// before\nvar times = default(Times); // Kind = 0 default may still be invalid if constructed raw\n// after\nvar times = Times.AtLeastOnce();","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try\n{\n    mock.Verify(m => m.Foo(), times);\n}\ncatch (InvalidOperationException ex)\n{\n    // invalid Times kind reached GetExceptionMessage; rebuild and retry\n    times = Times.AtLeastOnce();\n    mock.Verify(m => m.Foo(), times);\n}","preventionTips":["Construct Times only through public static factories","Avoid reflection or deserialization of Moq internal types","Keep Moq versions consistent across test projects"],"tags":["invalid-enum-value","moq","internal-invariant"],"backgroundTag":"invalid-enum-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"}