bchavez/Bogus · error · ArgumentException

Cannot find a method '{methodName}' that could accept {argum

Error message

Cannot find a method '{methodName}' that could accept {arguments.Length} arguments

What it means

Thrown by Tokenizer.FindMustashMethod (Tokenizer.cs:135). After the method name is known, Bogus tries to pick an overload whose parameter count can accept the supplied arguments (constraints: Method params.Length >= arg count, and optional args + arg count >= params.Length). If no registered overload satisfies both constraints, no overload matches and it throws. The name was valid; only the argument COUNT is wrong.

Source

Thrown at Source/Bogus/Tokenizer.cs:135

      {
         methodName = methodCall;
         arguments = new string[0];
      }

      methodName = methodName.ToUpperInvariant();
   }

   private static MustashMethod FindMustashMethod(string methodName, string[] arguments)
   {
      var selection =
         from mm in MustashMethods[methodName]
         orderby mm.Method.GetParameters().Count(pi => pi.IsOptional) - arguments.Length
         where mm.Method.GetParameters().Length >= arguments.Length
         where mm.OptionalArgs.Length + arguments.Length >= mm.Method.GetParameters().Length
         select mm;

      var found = selection.FirstOrDefault();
      return found ?? throw new ArgumentException($"Cannot find a method '{methodName}' that could accept {arguments.Length} arguments");
   }

   private static object[] ConvertStringArgumentsToObjects(string[] parameters, MustashMethod mm)
   {
      try
      {
         return mm.Method.GetParameters()
                         .Zip(parameters, GetValueForParameter)
                         .ToArray();
      }
      catch (OverflowException ex)
      {
         throw new ArgumentOutOfRangeException($"One of the arguments for {mm.Name} is out of supported range. Argument list: {string.Join(",", parameters)}", ex);
      }
      catch (Exception ex) when (ex is InvalidCastException or FormatException)
      {
         throw new ArgumentException($"One of the arguments for {mm.Name} cannot be converted to target type. Argument list: {string.Join(",", parameters)}", ex);
      }

View on GitHub (pinned to 6ece18c5c2)

Solutions

  1. Match the argument count to a real overload: check the dataset method signature (e.g. Randomizer.Number has 0-arg and 2-arg forms only).
  2. Drop extraneous args or add required ones so the count falls between the method's required-parameter count and its total-parameter count.
  3. Remember trailing optional parameters may be omitted, but you can never EXCEED the method's total parameter count.

Example fix

// before
faker.Parse("{{random.number(1,2,3)}}");
// after
faker.Parse("{{random.number(1,100)}}");
Defensive patterns

Strategy: validation

Validate before calling

// Check that the argument count you intend matches at least one registered overload.
static bool ArgCountOk(string categoryMethod, int argCount)
{
    var key = categoryMethod.ToUpperInvariant();
    if (!Tokenizer.MustashMethods.Contains(key)) return false;
    return Tokenizer.MustashMethods[key].Any(mm =>
        mm.Method.GetParameters().Length >= argCount &&
        mm.OptionalArgs.Length + argCount >= mm.Method.GetParameters().Length);
}

Try / catch

try { var s = faker.Parse(tpl); }
catch (ArgumentException ex) when (ex.Message.Contains("that could accept") && ex.Message.Contains("arguments"))
{
    // Argument-count mismatch; re-render with the offending template for diagnostics.
    throw new InvalidOperationException($"Bad arg count in template: {tpl}", ex);
}

Prevention

When it happens

Trigger: A {{category.method(a,b,c)}} whose comma-separated argument count matches no overload. Randomizer.Number exposes Number() and Number(int min,int max), so {{random.number(5)}} (1 arg) and {{random.number(1,2,3)}} (3 args) both fail; {{name.firstname("x")}} fails because FirstName takes 0 args.

Common situations: Assuming a single-argument convenience overload exists when only the 0-arg and 2-arg forms do; copy-pasting a template from docs of a different Bogus version where overloads changed; trailing empty-string arguments (the splitter uses RemoveEmptyEntries, but a stray comma in the middle still counts).

Related errors


AI-assisted analysis of bchavez/Bogus@6ece18c5c2 (2026-08-13). Data as JSON: /api/errors/d721767f30a42673. Report an issue: GitHub.