devlooped/moq · error · ArgumentException
Type does not have a default (public parameterless)…
Error message
Type {0} does not have a default (public parameterless) constructor. What it means
Moq must instantiate an instance of the given type (e.g. for Mock.Of<T> with constructor arguments, Delegate mocks, or creating mock instances) and requires a public parameterless constructor. Guard.CanCreateInstance validates this up front and throws ArgumentException naming the type when no default constructor exists.
Solutions
- Add a public parameterless constructor to the type.
- Provide the required constructor arguments via Mock<T>(object[] args) or use a factory instead of default instantiation.
- Mock the interface the type implements instead of the concrete type.
- Make the constructor public/accessible if it was private or internal by mistake.
Example fix
// before
class Service { public Service(IDep dep) { ... } }
var mock = new Mock<Service>(); // throws
// after
var mock = new Mock<Service>(new object[] { new Dep() });
// or: var mock = new Mock<IService>(); Defensive patterns
Strategy: validation
Validate before calling
bool canCreate = type.GetConstructor(BindingFlags.Public | BindingFlags.Instance, null, Type.EmptyTypes, null) != null;
if (!canCreate) throw new InvalidOperationException($"{type.Name} needs a public parameterless constructor or explicit args"); Type guard
bool HasDefaultCtor(Type t) => t.IsInterface || t.GetConstructor(Type.EmptyTypes)?.IsPublic == true;
Try / catch
try { var mock = new Mock<Service>(); }
catch (ArgumentException ex) when (ex.Message.Contains("default (public parameterless) constructor")) { var mock = new Mock<Service>(new object[] { deps }); } Prevention
- Prefer mocking interfaces over concrete classes.
- Provide constructor args via Mock<T>(object[]) when the type has required dependencies.
- Avoid mocking types with private constructors (singletons).
- Check Activator.CreateInstance(type) would succeed before mocking.
When it happens
Trigger: Passing a type to an API that creates instances — e.g. new Mock<T> with constructor-argument-less instantiation paths, mock.Create, or guarded instantiation helpers — where T only has parameterized, private, internal, or no explicit constructors.
Common situations: Mocking classes with required constructor parameters (DI-injected services), types with private constructors (singletons, static-holder classes), or structs/classes without an accessible public default constructor.
Related errors
- Value cannot be null. (Parameter 'newExpression')
- No constructor call could be found.
- A matching constructor for the given arguments was not…
- Unsupported expression
- Could not determine the correct positions for all argument…
AI-assisted analysis of devlooped/moq@89a5be629c (2026-09-16).
Data as JSON: /api/errors/b3ee2a37202889e8.
Report an issue: GitHub.
Appendix: source
Thrown at src/Moq/Guard.cs:24
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Linq.Expressions;
using System.Reflection;
using Moq.Properties;
using TypeNameFormatter;
namespace Moq
{
[DebuggerStepThrough]
static class Guard
{
public static void CanCreateInstance(Type type)
{
if (!type.CanCreateInstance())
{
throw new ArgumentException(
string.Format(
CultureInfo.CurrentCulture,
Resources.TypeHasNoDefaultConstructor,
type.GetFormattedName()));
}
}
public static void ImplementsInterface(Type interfaceType, Type type, string? paramName = null)
{
Debug.Assert(interfaceType != null);
Debug.Assert(interfaceType.IsInterface);
Debug.Assert(type != null);
if (interfaceType.IsAssignableFrom(type) == false)
{
throw new ArgumentException(
string.Format(View on GitHub (pinned to 89a5be629c)