{"record":{"id":"f9a5aa5b2a2f18e3","repo":"devlooped/moq","slug":"condition-argument-is-null","errorCode":null,"errorMessage":"condition (Argument is null)","messagePattern":"condition \\(Argument is null\\)","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"src/Moq/Mock`1.cs","lineNumber":690,"sourceCode":"        #endregion\n\n        #region When\n\n        /// <summary>\n        ///   Allows setting up a conditional setup.\n        ///   Conditional setups are only matched by an invocation\n        ///   when the specified condition evaluates to <see langword=\"true\"/>\n        ///   at the time when the invocation occurs.\n        /// </summary>\n        /// <param name=\"condition\">\n        ///   The condition that should be checked\n        ///   when a setup is being matched against an invocation.\n        /// </param>\n        public ISetupConditionResult<T> When(Func<bool> condition)\n        {\n            if (condition == null)\n            {\n                throw new ArgumentNullException(nameof(condition));\n            }\n\n            return new WhenPhrase<T>(this, new Condition(condition));\n        }\n\n        #endregion\n\n        #region Verify\n\n        /// <summary>\n        ///   Verifies that a specific invocation matching the given expression was performed on the mock.\n        ///   Use in conjunction with the default <see cref=\"MockBehavior.Loose\"/>.\n        /// </summary>\n        /// <param name=\"expression\">Expression to verify.</param>\n        /// <exception cref=\"MockException\">The invocation was not performed on the mock.</exception>\n        /// <example group=\"verification\">\n        ///   This example assumes that the mock has been used, and later we want to verify\n        ///   that a given invocation with specific parameters was performed:","sourceCodeStart":672,"sourceCodeEnd":708,"githubUrl":"https://github.com/devlooped/moq/blob/89a5be629c752960fe403e95ff583e4ae6f00542/src/Moq/Mock`1.cs#L672-L708","documentation":"Moq throws this ArgumentNullException when a null condition delegate is passed to Mock<T>.When(Func<bool> condition). The condition is evaluated to decide whether a conditional setup applies, so a null predicate is invalid.","triggerScenarios":"mock.When(null) — any call to When whose condition argument is null (src/Moq/Mock`1.cs:690).","commonSituations":"Storing the predicate in a variable that is never assigned or returned null from a condition-factory; refactoring that removed the lambda body but left a null literal.","solutions":["Pass a real predicate: mock.When(() => someState).Setup(...).","Null-check condition factory results before passing them to When.","If no condition is needed, use plain Setup instead of When."],"exampleFix":"// before\nmock.When(null).Setup(m => m.Foo()).Returns(1);\n// after\nmock.When(() => isEnabled).Setup(m => m.Foo()).Returns(1);","handlingStrategy":"type-guard","validationCode":"if (condition is null) throw new ArgumentNullException(nameof(condition));","typeGuard":"bool IsValidCondition(Func<bool> c) => c is not null;","tryCatchPattern":"try { mock.When(condition).Setup(...); } catch (ArgumentNullException) { mock.Setup(...); }","preventionTips":["Null-check predicates returned from condition factories","Prefer inline lambdas over nullable Func variables","Use plain Setup when no condition is required"],"tags":["moq","null-argument","when","condition"],"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"}