{"record":{"id":"cc796730e9ce02a9","repo":"devlooped/moq","slug":"argumentoutofrangeexception-callcount-0-value-must-be-times","errorCode":null,"errorMessage":"ArgumentOutOfRangeException: callCount ('{0}' value must be greater than or equal to '0').","messagePattern":"ArgumentOutOfRangeException: callCount \\('(.+?)' value must be greater than or equal to '0'\\)\\.","errorType":"validation","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"src/Moq/Times.cs","lineNumber":90,"sourceCode":"        ///   Specifies that a mocked method should be invoked one time as minimum.\n        /// </summary>\n        /// <returns>An object defining the allowed number of invocations.</returns>\n        public static Times AtLeastOnce()\n        {\n            return new Times(Kind.AtLeastOnce, 1, int.MaxValue);\n        }\n\n        /// <summary>\n        ///   Specifies that a mocked method should be invoked <paramref name=\"callCount\"/> times\n        ///   as maximum.\n        /// </summary>\n        /// <param name=\"callCount\">The maximum number of times.</param>\n        /// <returns>An object defining the allowed number of invocations.</returns>\n        public static Times AtMost(int callCount)\n        {\n            if (callCount < 0)\n            {\n                throw new ArgumentOutOfRangeException(nameof(callCount));\n            }\n\n            return new Times(Kind.AtMost, 0, callCount);\n        }\n\n        /// <summary>\n        ///   Specifies that a mocked method should be invoked one time as maximum.\n        /// </summary>\n        /// <returns>An object defining the allowed number of invocations.</returns>\n        public static Times AtMostOnce()\n        {\n            return new Times(Kind.AtMostOnce, 0, 1);\n        }\n\n        /// <summary>\n        ///   Specifies that a mocked method should be invoked between\n        ///   <paramref name=\"callCountFrom\"/> and <paramref name=\"callCountTo\"/> times.\n        /// </summary>","sourceCodeStart":72,"sourceCodeEnd":108,"githubUrl":"https://github.com/devlooped/moq/blob/89a5be629c752960fe403e95ff583e4ae6f00542/src/Moq/Times.cs#L72-L108","documentation":"AtMost builds the upper bound of an invocation-count constraint used in Verify, so a negative count is meaningless and would invert the allowed range (0..callCount), silently breaking verification; the factory rejects it eagerly at construction. The throw site is a plain argument-validation guard: the faulty input is an int callCount argument less than zero, e.g. mock.Verify(m => m.Foo(), Times.AtMost(-1)), often from a computed count that went below zero. Note this site throws the plain ArgumentOutOfRangeException(nameof(callCount)); the richer formatted message ('{0}' value must be greater than or equal to '0') belongs to a different constructor overload in the same type.","triggerScenarios":"Calling Times.AtMost with a negative number, e.g. from an off-by-one subtraction, a default of -1 meaning 'unset', or user-supplied config.","commonSituations":"Uninitialized integer defaults (-1) fed into AtMost; subtracting counts; parsing configuration where sentinel negatives leak through.","solutions":["Validate/clamp the count to >= 0 before calling AtMost","Replace sentinel -1 with a proper optional/nullable representation and choose Times accordingly","Use Times.AtMost(0) or Times.Never for zero expected calls"],"exampleFix":"// before\nmock.Verify(m => m.Foo(), Times.AtMost(limit)); // limit default -1\n// after\nmock.Verify(m => m.Foo(), limit < 0 ? Times.Never : Times.AtMost(limit));","handlingStrategy":"validation","validationCode":"if (maxCalls >= 0)\n    mock.Verify(m => m.Foo(), Times.AtMost(maxCalls));","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Clamp upper bounds to >= 0","Replace -1 sentinels with nullable ints","Use AtMost(0)/Never for zero expected calls"],"tags":["argument-out-of-range","moq","verification"],"backgroundTag":"argument-out-of-range","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"}