{"record":{"id":"43372ec07c15e006","repo":"devlooped/moq","slug":"argumentnullexception-value-cannot-be-null-parameter-random","errorCode":null,"errorMessage":"ArgumentNullException: Value cannot be null. (Parameter 'random')","messagePattern":"ArgumentNullException: Value cannot be null\\. \\(Parameter 'random'\\)","errorType":"validation","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"src/Moq/ReturnsExtensions.cs","lineNumber":195,"sourceCode":"        /// Allows to specify the delayed return value of an asynchronous method.\n        /// </summary>\n        public static IReturnsResult<TMock> ReturnsAsync<TMock, TResult>(this IReturns<TMock, ValueTask<TResult>> mock,\n            TResult value, TimeSpan minDelay, TimeSpan maxDelay) where TMock : class\n        {\n            var delay = GetDelay(minDelay, maxDelay, Random);\n\n            return DelayedResult(mock, value, delay);\n        }\n\n        /// <summary>\n        /// <para>Allows to specify the delayed return value of an asynchronous method.</para>\n        /// <para>Use the <see cref=\"Random\"/> argument to pass in (seeded) random generators used across your unit test.</para>\n        /// </summary>\n        public static IReturnsResult<TMock> ReturnsAsync<TMock, TResult>(this IReturns<TMock, Task<TResult>> mock,\n            TResult value, TimeSpan minDelay, TimeSpan maxDelay, Random random) where TMock : class\n        {\n            if (random == null)\n                throw new ArgumentNullException(nameof(random));\n\n            var delay = GetDelay(minDelay, maxDelay, random);\n\n            return DelayedResult(mock, value, delay);\n        }\n\n        /// <summary>\n        /// <para>Allows to specify the delayed return value of an asynchronous method.</para>\n        /// <para>Use the <see cref=\"Random\"/> argument to pass in (seeded) random generators used across your unit test.</para>\n        /// </summary>\n        public static IReturnsResult<TMock> ReturnsAsync<TMock, TResult>(this IReturns<TMock, ValueTask<TResult>> mock,\n            TResult value, TimeSpan minDelay, TimeSpan maxDelay, Random random) where TMock : class\n        {\n            if (random == null)\n                throw new ArgumentNullException(nameof(random));\n\n            var delay = GetDelay(minDelay, maxDelay, random);\n","sourceCodeStart":177,"sourceCodeEnd":213,"githubUrl":"https://github.com/devlooped/moq/blob/89a5be629c752960fe403e95ff583e4ae6f00542/src/Moq/ReturnsExtensions.cs#L177-L213","documentation":"This ReturnsAsync overload for Task<TResult> with randomized min/max delay requires a non-null Random instance because it generates a random delay via GetDelay(minDelay, maxDelay, random). Passing null throws ArgumentNullException with parameter name 'random' immediately at setup time.","triggerScenarios":"mock.Setup(x => x.GetDataAsync()).ReturnsAsync(value, minDelay, maxDelay, null) — the four-argument overload with a null Random.","commonSituations":"Refactoring from the no-arg ReturnsAsync(value) overload to the delayed overload and passing null 'just to get defaults'; test data builders supplying a null Random.","solutions":["Pass a Random instance, e.g. new Random(12345) for a reproducible seeded generator","If no randomness is wanted, use the ReturnsAsync(value, delay) overload with a fixed TimeSpan instead"],"exampleFix":"// before\nmock.Setup(m => m.GetAsync()).ReturnsAsync(result, TimeSpan.Zero, TimeSpan.FromSeconds(1), null);\n// after\nmock.Setup(m => m.GetAsync()).ReturnsAsync(result, TimeSpan.Zero, TimeSpan.FromSeconds(1), new Random(42));","handlingStrategy":"validation","validationCode":"if (random is null) throw new ArgumentException(\"ReturnsAsync with min/max delay requires a Random instance\");","typeGuard":"bool HasRandom(Random? r) => r is not null;","tryCatchPattern":"try { mock.Setup(m => m.GetAsync()).ReturnsAsync(v, min, max, random); } catch (ArgumentNullException ex) when (ex.ParamName == \"random\") { /* pass a Random or fixed delay */ }","preventionTips":["Always pass a seeded Random for deterministic tests","Prefer fixed-delay overloads when randomness is not needed","Null-check optional collaborators before building the setup"],"tags":["moq","null-argument","async","randomness"],"backgroundTag":"null-argument","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"}