devlooped/moq · error · ArgumentException

Resources.SetupNotEventAdd (formatted with part.Expression)

Error message

Resources.SetupNotEventAdd (formatted with part.Expression)

What it means

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.

Solutions

  1. 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)
  2. Ensure the event is declared on a non-sealed type visible to Moq's reflection with the expected binding flags
  3. Avoid 'new' hiding of events; use a single declaration and override/invoke from the base
  4. Fire the event from the production code path (or a protected virtual OnXxx method) instead of Mock.Raise

Example fix

// before
mock.Raise(x => x.ExplicitEvent += null, EventArgs.Empty); // explicit interface impl -> throws
// after
mock.As<IMyEvents>().Raise(e => e.MyEvent += null, EventArgs.Empty);
Defensive patterns

Strategy: try-catch

Validate before calling

var evt = typeof(T).GetEvents().FirstOrDefault(e => e.GetAddMethod(true) == typeof(T).GetMethod("add_MyEvent", BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance));
// if evt is null, Raise will fail; use mock.As<IDeclaringInterface>().Raise instead

Try / catch

try
{
    mock.Raise(x => x.MyEvent += null, EventArgs.Empty);
}
catch (ArgumentException ex) when (ex.Message.Contains("SetupNotEventAdd"))
{
    mock.As<IDeclaringInterface>().Raise(e => e.MyEvent += null, EventArgs.Empty);
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Understand the failure class

Background: 'Could not be found', 'does not exist', 'not found in database': the resource-not-found family when an ID, slug, key, or URI lookup comes back empty — this error's family across 20 libraries.

Related errors


AI-assisted analysis of devlooped/moq@89a5be629c (2026-09-16). Data as JSON: /api/errors/da5d4a4ad5163965. Report an issue: GitHub.

Appendix: source

Thrown at src/Moq/Mock.cs:726

            return (Task)Mock.RaiseEvent(mock, expression, parts, arguments);
        }

        internal static object? RaiseEvent(Mock mock, LambdaExpression expression, Stack<MethodExpectation> parts, object?[] arguments)
        {
            const BindingFlags bindingFlags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly;

            var part = parts.Pop();
            var method = part.Method;

            if (parts.Count == 0)
            {
                EventInfo @event;
                if (method.IsEventAddAccessor())
                {
                    var implementingMethod = method.GetImplementingMethod(mock.Object.GetType());
                    @event = implementingMethod.DeclaringType!.GetEvents(bindingFlags)
                                               .SingleOrDefault(e => e.GetAddMethod(true) == implementingMethod)
                             ?? throw new ArgumentException(string.Format(CultureInfo.CurrentCulture,
                                                                          Resources.SetupNotEventAdd,
                                                                          part.Expression));

                }
                else if (method.IsEventRemoveAccessor())
                {
                    var implementingMethod = method.GetImplementingMethod(mock.Object.GetType());
                    @event = implementingMethod.DeclaringType!.GetEvents(bindingFlags)
                                               .SingleOrDefault(e => e.GetRemoveMethod(true) == implementingMethod)
                             ?? throw new ArgumentException(string.Format(CultureInfo.CurrentCulture,
                                                                          Resources.SetupNotEventRemove,
                                                                          part.Expression));

                }
                else
                {
                    throw new ArgumentException(
                        string.Format(

View on GitHub (pinned to 89a5be629c)