devlooped/moq · error · Exception
Unhandled binding type
Error message
Unhandled binding type: {0} What it means
When rendering a MemberInitExpression or ListInitExpression as a string, Moq iterates the MemberBinding list and switches on binding.BindingType (MemberAssignment, MemberMemberBinding, MemberListBinding). An unrecognized binding type hits the default case and throws this Exception. It indicates a binding kind the stringifier does not handle.
Solutions
- Flatten or simplify the object initializer in the setup/verify expression so only plain assignments are used
- Assign nested objects before the expression or inside a helper method instead of via nested initializers
- Report/patch the missing BindingType case in Moq's AppendExpression binding switch
Example fix
// before
mock.Verify(m => m.Foo(new Bar { Inner = { Deep = { X = 1 } } }));
// after
var bar = new Bar();
bar.Inner.Deep.X = 1;
mock.Verify(m => m.Foo(bar)); Defensive patterns
Strategy: validation
Validate before calling
// use only simple property assignments in initializers inside expressions
mock.Verify(m => m.Foo(new Bar { A = 1, B = 2 })); // fine
// avoid deeply nested member/list initializers inside the lambda Prevention
- Prefer building complex objects outside the expression
- Keep initializers one level deep in Moq expressions
- Re-test expression-heavy verifications after compiler upgrades
When it happens
Trigger: Setup/Verify/Of expressions containing nested member initializers whose binding type falls outside the three handled kinds — typically from unusual object-initializer shapes or compiler-generated bindings.
Common situations: Deeply nested object initializers in mocked type setups; code generated by tools; using C# features that produce binding kinds the formatter predates.
Understand the failure class
Background: UnsupportedOperationException and "is not supported" errors: when a library deliberately refuses a call — this error's family across 30 libraries.
Related errors
- Unhandled expression type
- Unsupported expression
- Expression is not a setter
- Expression is not an event add
- Expression is not an event remove
AI-assisted analysis of devlooped/moq@89a5be629c (2026-09-16).
Data as JSON: /api/errors/024eb9dddafb6c1b.
Report an issue: GitHub.
Appendix: source
Thrown at src/Moq/StringBuilderExtensions.AppendExpression.cs:438
case MemberBindingType.Assignment:
var assignment = (MemberAssignment)binding;
return builder.Append(assignment.Member.Name)
.Append("= ")
.AppendExpression(assignment.Expression);
case MemberBindingType.MemberBinding:
return b.AppendCommaSeparated(((MemberMemberBinding)binding).Bindings, AppendMemberBinding);
case MemberBindingType.ListBinding:
var original = ((MemberListBinding)binding).Initializers;
for (int i = 0, n = original.Count; i < n; i++)
{
builder.AppendElementInit(original[i]);
}
return builder;
default:
throw new Exception(string.Format(Resources.UnhandledBindingType, binding.BindingType));
}
}
}
static StringBuilder AppendExpression(this StringBuilder builder, ListInitExpression expression)
{
return builder.AppendExpression(expression.NewExpression)
.AppendCommaSeparated(" { ", expression.Initializers, AppendElementInit, " }");
}
static StringBuilder AppendExpression(this StringBuilder builder, MatchExpression expression)
{
return builder.AppendExpression(expression.Match.RenderExpression);
}
}
}
View on GitHub (pinned to 89a5be629c)