{"record":{"id":"da5d4a4ad5163965","repo":"devlooped/moq","slug":"resources-setupnoteventadd-formatted-with-part-expression","errorCode":null,"errorMessage":"Resources.SetupNotEventAdd (formatted with part.Expression)","messagePattern":"Resources\\.SetupNotEventAdd \\(formatted with part\\.Expression\\)","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Moq/Mock.cs","lineNumber":726,"sourceCode":"            return (Task)Mock.RaiseEvent(mock, expression, parts, arguments);\n        }\n\n        internal static object? RaiseEvent(Mock mock, LambdaExpression expression, Stack<MethodExpectation> parts, object?[] arguments)\n        {\n            const BindingFlags bindingFlags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly;\n\n            var part = parts.Pop();\n            var method = part.Method;\n\n            if (parts.Count == 0)\n            {\n                EventInfo @event;\n                if (method.IsEventAddAccessor())\n                {\n                    var implementingMethod = method.GetImplementingMethod(mock.Object.GetType());\n                    @event = implementingMethod.DeclaringType!.GetEvents(bindingFlags)\n                                               .SingleOrDefault(e => e.GetAddMethod(true) == implementingMethod)\n                             ?? throw new ArgumentException(string.Format(CultureInfo.CurrentCulture,\n                                                                          Resources.SetupNotEventAdd,\n                                                                          part.Expression));\n\n                }\n                else if (method.IsEventRemoveAccessor())\n                {\n                    var implementingMethod = method.GetImplementingMethod(mock.Object.GetType());\n                    @event = implementingMethod.DeclaringType!.GetEvents(bindingFlags)\n                                               .SingleOrDefault(e => e.GetRemoveMethod(true) == implementingMethod)\n                             ?? throw new ArgumentException(string.Format(CultureInfo.CurrentCulture,\n                                                                          Resources.SetupNotEventRemove,\n                                                                          part.Expression));\n\n                }\n                else\n                {\n                    throw new ArgumentException(\n                        string.Format(","sourceCodeStart":708,"sourceCodeEnd":744,"githubUrl":"https://github.com/devlooped/moq/blob/89a5be629c752960fe403e95ff583e4ae6f00542/src/Moq/Mock.cs#L708-L744","documentation":"Mock.Raise<T>(event, args) / RaiseEvent resolves the event from the add accessor of the invoked expression. When the expression's method is an event add accessor but no declared event on the mock's type has that method as its add method, Moq cannot identify the target event and throws ArgumentException with Resources.SetupNotEventAdd.","triggerScenarios":"Calling mock.Raise(x => x.SomeEvent += null, args) where the event is an explicitly implemented interface event, is new-hidden in a derived type, or the resolving binder (bindingFlags) does not surface the declaring type's events, so SingleOrDefault finds no matching EventInfo.","commonSituations":"Raising events that are explicit interface implementations; events redeclared with 'new' hiding a base event; mocking a class where the event accessor was compiled differently than expected.","solutions":["Raise the event via the interface type: cast the expression to the interface that declares the event, e.g. mock.As<IMyEvents>().Raise(e => e.MyEvent += null, args)","Ensure the event is declared on a non-sealed type visible to Moq's reflection with the expected binding flags","Avoid 'new' hiding of events; use a single declaration and override/invoke from the base","Fire the event from the production code path (or a protected virtual OnXxx method) instead of Mock.Raise"],"exampleFix":"// before\nmock.Raise(x => x.ExplicitEvent += null, EventArgs.Empty); // explicit interface impl -> throws\n// after\nmock.As<IMyEvents>().Raise(e => e.MyEvent += null, EventArgs.Empty);","handlingStrategy":"try-catch","validationCode":"var evt = typeof(T).GetEvents().FirstOrDefault(e => e.GetAddMethod(true) == typeof(T).GetMethod(\"add_MyEvent\", BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance));\n// if evt is null, Raise will fail; use mock.As<IDeclaringInterface>().Raise instead","typeGuard":null,"tryCatchPattern":"try\n{\n    mock.Raise(x => x.MyEvent += null, EventArgs.Empty);\n}\ncatch (ArgumentException ex) when (ex.Message.Contains(\"SetupNotEventAdd\"))\n{\n    mock.As<IDeclaringInterface>().Raise(e => e.MyEvent += null, EventArgs.Empty);\n}","preventionTips":["Avoid explicit interface event implementations when you plan to use Mock.Raise","Do not hide base events with 'new'","Raise events through a protected OnXxx helper in the class instead of reflection-based Raise","Ensure mocked types and their events are accessible to DynamicProxyGenAssembly2"],"tags":["moq","event","raise","setup"],"backgroundTag":"resource-not-found","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"}