{"record":{"id":"511bcc1679601aba","repo":"devlooped/moq","slug":"value-cannot-be-an-empty-string","errorCode":null,"errorMessage":"Value cannot be an empty string.","messagePattern":"Value cannot be an empty string\\.","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Moq/Guard.cs","lineNumber":195,"sourceCode":"                throw new ArgumentNullException(paramName);\n            }\n        }\n\n        /// <summary>\n        /// Ensures the given string <paramref name=\"value\"/> is not null or empty.\n        /// Throws <see cref=\"ArgumentNullException\"/> in the first case, or \n        /// <see cref=\"ArgumentException\"/> in the latter.\n        /// </summary>\n        public static void NotNullOrEmpty(string value, string paramName)\n        {\n            if (value == null)\n            {\n                throw new ArgumentNullException(paramName);\n            }\n\n            if (value.Length == 0)\n            {\n                throw new ArgumentException(Resources.ArgumentCannotBeEmpty, paramName);\n            }\n        }\n\n        public static void NotField(MemberExpression memberAccess)\n        {\n            if (memberAccess.Member is FieldInfo)\n                throw new NotSupportedException(\n                    string.Format(\n                        Resources.FieldsNotSupported,\n                        memberAccess.ToStringFixed()));\n        }\n\n        public static void IsMockable(Type type)\n        {\n            if (!type.IsMockable())\n            {\n                throw new NotSupportedException(\n                    string.Format(","sourceCodeStart":177,"sourceCodeEnd":213,"githubUrl":"https://github.com/devlooped/moq/blob/89a5be629c752960fe403e95ff583e4ae6f00542/src/Moq/Guard.cs#L177-L213","documentation":"Guard.NotNullOrEmpty validates that a string argument is non-null and non-empty before Moq proceeds. After the null check throws ArgumentNullException, an empty string (Length == 0) throws ArgumentException with \"Value cannot be an empty string.\". It protects APIs that require a meaningful string identifier (e.g. member or event names).","triggerScenarios":"Passing string.Empty to any Moq API routed through this guard, e.g. empty property/member names in weak-event or reflection-based helpers, empty event names in Raise/Event setups.","commonSituations":"Building names dynamically from config or reflection and getting an empty result; default-initialized string fields; copy-paste leaving a placeholder empty.","solutions":["Pass a non-empty string value (trim user/config input before calling).","Default the value to a valid fallback when it would be empty.","If null vs empty semantics differ, ensure the caller passes null (which throws ArgumentNullException instead) only when the API allows it.","Validate inputs at your API boundary before invoking Moq."],"exampleFix":"// before\nstring name = config.MemberName ?? \"\"; // may be \"\"\nmoqHelper.Invoke(name); // throws\n// after\nstring name = config.MemberName;\nif (string.IsNullOrEmpty(name)) throw new InvalidOperationException(\"MemberName not configured\");\nmoqHelper.Invoke(name);","handlingStrategy":"validation","validationCode":"if (string.IsNullOrEmpty(name)) throw new InvalidOperationException(\"Name must be non-empty before calling the API\");","typeGuard":"static string RequireNonEmpty(string? s, [CallerArgumentExpression(nameof(s))] string? p = null) => string.IsNullOrEmpty(s) ? throw new ArgumentException(\"Value cannot be empty\", p) : s;","tryCatchPattern":"try { api.Invoke(name); }\ncatch (ArgumentException ex) when (ex.Message == \"Value cannot be an empty string.\") { /* supply a valid name */ throw; }","preventionTips":["Validate/trim string inputs at your boundary before invoking library APIs.","Never default string identifiers to string.Empty; use null-with-check or a valid sentinel.","Add unit tests covering empty-string inputs to your configuration paths."],"tags":["moq","argument-validation","empty-string"],"backgroundTag":"empty-required-field","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"}