Skip to content

Mock

ClassMockMagiccom.wetzold.mock-magic
public static class Mock

Provides static generators for mock values, formatted strings, token templates, themed text, and image placeholders.

public static void RegisterToken(string name, Func<string> factory)

Register (or replace) a string-returning generator. The same name becomes usable as a {{name}} token inside Mock.Parse(string).

Parameters

NameType
nameSystem.String
factorySystem.Func{System.String}
public static void RegisterGenerator<T>(string name, Func<T> factory)

Register a typed generator. Works for any T. If T is string the generator also becomes a Mock.Parse(string) token.

Parameters

NameType
nameSystem.String
factorySystem.Func{{T}}
public static T Get<T>(string name)

Resolve a previously registered typed generator. Throws if no generator was registered for the T/name combination.

Parameters

NameType
nameSystem.String

Returns

{T}

public static bool UnregisterToken(string name)

Remove everything registered under name: the {{name}} token and every typed generator from Mock.RegisterGenerator<T>(string, Func<T>), whatever its type. Names match case-insensitively, like tokens. Built-in tokens can be removed too, for example before registering a replacement. Returns true when anything was removed.

Parameters

NameType
nameSystem.String

Returns

System.Boolean

public static bool HasToken(string name)

Check whether a token has been registered.

Parameters

NameType
nameSystem.String

Returns

System.Boolean

public static string Parse(string template)

Replace every {{tokenName}} in template with the current output of the matching registered generator. Unknown tokens are left as-is so they can be easily spotted during debugging.

Parameters

NameType
templateSystem.String

Returns

System.String

Examples

Mock.Parse(“{{firstName}} <{{email}}> id={{unique}}”)

public static string[] Parse(string template, int count)

Parse template repeatedly, returning one string per iteration.

Parameters

NameType
templateSystem.String
countSystem.Int32

Returns

System.String[]

public static string HackerAdjective()

Random tech-flavoured adjective, e.g. “cross-platform”.

Returns

System.String

public static string HackerNoun()

Random tech-flavoured noun, e.g. “firewall”.

Returns

System.String

public static string HackerVerb()

Random tech-flavoured verb (imperative), e.g. “bypass”.

Returns

System.String

public static string HackerIngVerb()

Random tech-flavoured verb in -ing form, e.g. “bypassing”.

Returns

System.String

public static string HackerPhrase()

Assemble a phrase like “try parsing the SSL sensor, maybe it will reboot the digital bus!”.

Returns

System.String

public static string Guid()

Generate a random GUID string. When a seed is active the GUID is produced from MockRandom so the output is reproducible; otherwise the runtime’s Guid.NewGuid() is used for uniqueness guarantees.

Returns

System.String

public static string[] Guids(int count)

Generate multiple random GUIDs.

Parameters

NameType
countSystem.Int32

Returns

System.String[]

public static int Id()

Generate a sequential ID using the shared MockRandom.UniqueIndex.

Returns

System.Int32

public static int[] Ids(int count)

Generate multiple sequential IDs.

Parameters

NameType
countSystem.Int32

Returns

System.Int32[]

public static void ResetIdCounter(int startValue = 1)

Reset the shared ID counter so the next Mock.Id() (and {{unique}} token) returns startValue. Any value works, including zero and negative numbers. Randomness and an active seed are unchanged. Mock.Seed(int) restarts IDs at 1, so call this after it for another start.

Parameters

NameType
startValueSystem.Int32
public static void UseDateReference(DateTime? reference)

Set a fixed reference date for date/time generators. Pass null to restore wall-clock behaviour.

Parameters

NameType
referenceSystem.Nullable{System.DateTime}
public static void UseDateReferenceProvider(Func<DateTime> provider)

Set a dynamic reference-date provider for date/time generators. Pass null to restore wall-clock behaviour.

Parameters

NameType
providerSystem.Func{System.DateTime}
public static long Timestamp()

Generate a Unix timestamp (reference time with random offset of up to a year in each direction).

Returns

System.Int64

public static long[] Timestamps(int count)

Generate multiple timestamps.

Parameters

NameType
countSystem.Int32

Returns

System.Int64[]

public static string Date(int yearsBack = 5, int yearsForward = 1)

Generate a random date string.

Parameters

NameType
yearsBackSystem.Int32
yearsForwardSystem.Int32

Returns

System.String

public static string[] Dates(int count, int yearsBack = 5, int yearsForward = 1)

Generate multiple random dates.

Parameters

NameType
countSystem.Int32
yearsBackSystem.Int32
yearsForwardSystem.Int32

Returns

System.String[]

public static string Time()

Generate a random time string (HH:mm:ss).

Returns

System.String

public static string[] Times(int count)

Generate multiple random times.

Parameters

NameType
countSystem.Int32

Returns

System.String[]

public static string DateTime(int yearsBack = 5, int yearsForward = 1)

Generate a random datetime string.

Parameters

NameType
yearsBackSystem.Int32
yearsForwardSystem.Int32

Returns

System.String

public static string[] DateTimes(int count, int yearsBack = 5, int yearsForward = 1)

Generate multiple random datetimes.

Parameters

NameType
countSystem.Int32
yearsBackSystem.Int32
yearsForwardSystem.Int32

Returns

System.String[]

public static string Name()

Generate a random full name.

Returns

System.String

public static string[] Names(int count)

Generate multiple random names.

Parameters

NameType
countSystem.Int32

Returns

System.String[]

public static string FirstName()

Generate a random first name.

Returns

System.String

public static string[] FirstNames(int count)

Generate multiple random first names.

Parameters

NameType
countSystem.Int32

Returns

System.String[]

public static string LastName()

Generate a random last name.

Returns

System.String

public static string[] LastNames(int count)

Generate multiple random last names.

Parameters

NameType
countSystem.Int32

Returns

System.String[]

public static string Email()

Generate a random email address.

Returns

System.String

public static string[] Emails(int count)

Generate multiple random emails.

Parameters

NameType
countSystem.Int32

Returns

System.String[]

public static string Phone()

Generate a random phone number in the shape +1 (###) ###-####.

Returns

System.String

public static string Phone(bool fictional)

Generate a US-format phone number. With fictional, the number uses 555-0100 to 555-0199, the range reserved for fiction, so it cannot reach a real person; otherwise it is any valid-looking number.

Parameters

NameType
fictionalSystem.Boolean

Returns

System.String

public static string[] Phones(int count)

Generate multiple random phone numbers.

Parameters

NameType
countSystem.Int32

Returns

System.String[]

public static string Address()

Generate a random street address.

Returns

System.String

public static string[] Addresses(int count)

Generate multiple random addresses.

Parameters

NameType
countSystem.Int32

Returns

System.String[]

public static string City()

Generate a random city name.

Returns

System.String

public static string[] Cities(int count)

Generate multiple random cities.

Parameters

NameType
countSystem.Int32

Returns

System.String[]

public static string Country()

Generate a random country name.

Returns

System.String

public static string[] Countries(int count)

Generate multiple random countries.

Parameters

NameType
countSystem.Int32

Returns

System.String[]

public static string ZipCode()

Generate a random 5-digit ZIP code.

Returns

System.String

public static string[] ZipCodes(int count)

Generate multiple random ZIP codes.

Parameters

NameType
countSystem.Int32

Returns

System.String[]

public static string Company()

Generate a random company name.

Returns

System.String

public static string[] Companies(int count)

Generate multiple random company names.

Parameters

NameType
countSystem.Int32

Returns

System.String[]

public static string JobTitle()

Generate a random job title.

Returns

System.String

public static string[] JobTitles(int count)

Generate multiple random job titles.

Parameters

NameType
countSystem.Int32

Returns

System.String[]

public static string LoremIpsum(int wordCount = 50)

Generate lorem ipsum text.

Parameters

NameType
wordCountSystem.Int32

Returns

System.String

public static string Word()

Generate a random word.

Returns

System.String

public static string[] Words(int count)

Generate multiple random words.

Parameters

NameType
countSystem.Int32

Returns

System.String[]

public static string Sentence(int minWords = 5, int maxWords = 15)

Generate a random sentence.

Parameters

NameType
minWordsSystem.Int32
maxWordsSystem.Int32

Returns

System.String

public static string[] Sentences(int count, int minWords = 5, int maxWords = 15)

Generate multiple random sentences.

Parameters

NameType
countSystem.Int32
minWordsSystem.Int32
maxWordsSystem.Int32

Returns

System.String[]

public static string Paragraph(int sentenceCount = 5)

Generate a random paragraph.

Parameters

NameType
sentenceCountSystem.Int32

Returns

System.String

public static string[] Paragraphs(int count, int sentenceCount = 5)

Generate multiple random paragraphs.

Parameters

NameType
countSystem.Int32
sentenceCountSystem.Int32

Returns

System.String[]

public static Color Color()

Generate a random Unity Color.

Returns

UnityEngine.Color

public static Color[] Colors(int count)

Generate multiple random colors.

Parameters

NameType
countSystem.Int32

Returns

UnityEngine.Color[]

public static string ColorName()

Generate a random color name.

Returns

System.String

public static string[] ColorNames(int count)

Generate multiple random color names.

Parameters

NameType
countSystem.Int32

Returns

System.String[]

public static string HexColor()

Generate a random hex color string.

Returns

System.String

public static string[] HexColors(int count)

Generate multiple random hex colors.

Parameters

NameType
countSystem.Int32

Returns

System.String[]

public static Vector2 Coordinate()

Generate a random coordinate (lat, lon).

Returns

UnityEngine.Vector2

public static Vector2[] Coordinates(int count)

Generate multiple random coordinates.

Parameters

NameType
countSystem.Int32

Returns

UnityEngine.Vector2[]

public static float Latitude()

Generate a random latitude.

Returns

System.Single

public static float Longitude()

Generate a random longitude.

Returns

System.Single

public static decimal Currency(decimal min = 0, decimal max = 10000)

Generate a random currency amount.

Parameters

NameType
minSystem.Decimal
maxSystem.Decimal

Returns

System.Decimal

public static decimal[] Currencies(int count, decimal min = 0, decimal max = 10000)

Generate multiple random currency amounts.

Parameters

NameType
countSystem.Int32
minSystem.Decimal
maxSystem.Decimal

Returns

System.Decimal[]

public static float Percentage()

Generate a random percentage (0-100).

Returns

System.Single

public static float[] Percentages(int count)

Generate multiple random percentages.

Parameters

NameType
countSystem.Int32

Returns

System.Single[]

public static bool Boolean()

Generate a random boolean.

Returns

System.Boolean

public static bool[] Booleans(int count)

Generate multiple random booleans.

Parameters

NameType
countSystem.Int32

Returns

System.Boolean[]

public static int Integer(int min = 0, int max = 100)

Generate a random integer within range.

Parameters

NameType
minSystem.Int32
maxSystem.Int32

Returns

System.Int32

public static int[] Integers(int count, int min = 0, int max = 100)

Generate multiple random integers.

Parameters

NameType
countSystem.Int32
minSystem.Int32
maxSystem.Int32

Returns

System.Int32[]

public static float Float(float min = 0, float max = 100)

Generate a random float within range.

Parameters

NameType
minSystem.Single
maxSystem.Single

Returns

System.Single

public static float[] Floats(int count, float min = 0, float max = 100)

Generate multiple random floats.

Parameters

NameType
countSystem.Int32
minSystem.Single
maxSystem.Single

Returns

System.Single[]

public static string Url()

Generate a random URL.

Returns

System.String

public static string[] Urls(int count)

Generate multiple random URLs.

Parameters

NameType
countSystem.Int32

Returns

System.String[]

public static string IpAddress()

Generate a random IP address.

Returns

System.String

public static string[] IpAddresses(int count)

Generate multiple random IP addresses.

Parameters

NameType
countSystem.Int32

Returns

System.String[]

public static string MacAddress()

Generate a random MAC address (uppercase hex, colon-separated).

Returns

System.String

public static string[] MacAddresses(int count)

Generate multiple random MAC addresses.

Parameters

NameType
countSystem.Int32

Returns

System.String[]

public static string Username()

Generate a random username.

Returns

System.String

public static string[] Usernames(int count)

Generate multiple random usernames.

Parameters

NameType
countSystem.Int32

Returns

System.String[]

public static string Password(int length = 12)

Generate a random password with guaranteed character diversity.

Parameters

NameType
lengthSystem.Int32

Returns

System.String

public static string[] Passwords(int count, int length = 12)

Generate multiple random passwords.

Parameters

NameType
countSystem.Int32
lengthSystem.Int32

Returns

System.String[]

public static string CreditCard()

Generate a random credit card number with valid Luhn checksum (fake, for testing only).

Returns

System.String

public static string[] CreditCards(int count)

Generate multiple random credit card numbers.

Parameters

NameType
countSystem.Int32

Returns

System.String[]

public static string Ssn()

Generate a random SSN-shaped string (fake, for testing only).

Returns

System.String

public static string[] Ssns(int count)

Generate multiple random SSNs.

Parameters

NameType
countSystem.Int32

Returns

System.String[]

public static string Isbn()

Generate a random ISBN-13 with valid check digit.

Returns

System.String

public static string[] Isbns(int count)

Generate multiple random ISBNs.

Parameters

NameType
countSystem.Int32

Returns

System.String[]

public static string State()

Generate a random US state name.

Returns

System.String

public static string[] States(int count)

Generate multiple random US state names.

Parameters

NameType
countSystem.Int32

Returns

System.String[]

public static string Domain()

Generate a random domain name (e.g., “cloudpulse.io”).

Returns

System.String

public static string[] DomainNames(int count)

Generate multiple random domain names.

Parameters

NameType
countSystem.Int32

Returns

System.String[]

public static string FullAddress()

Generate a random US full address whose city, state and ZIP code belong together (e.g., “1234 Oak Ave, Seattle, WA 98101”).

Returns

System.String

public static string[] FullAddresses(int count)

Generate multiple random full addresses.

Parameters

NameType
countSystem.Int32

Returns

System.String[]

public static int Age()

Generate a realistic adult age drawn from a Gaussian distribution (mean 38, stddev 14), clamped to the range 18-85.

Returns

System.Int32

public static int[] Ages(int count)

Generate multiple random ages.

Parameters

NameType
countSystem.Int32

Returns

System.Int32[]

public static string ProductName()

Generate a random product name (e.g., “Ultra Wireless Headphones”).

Returns

System.String

public static string[] ProductNames(int count)

Generate multiple random product names.

Parameters

NameType
countSystem.Int32

Returns

System.String[]

public static string Price(float min = 0.99, float max = 999.99)

Generate a random price string (e.g., “$49.99”). A data locale scales the amount to its currency’s typical magnitude, so 29 reads ₩29,000 in Korean.

Parameters

NameType
minSystem.Single
maxSystem.Single

Returns

System.String

public static string[] Prices(int count, float min = 0.99, float max = 999.99)

Generate multiple random prices.

Parameters

NameType
countSystem.Int32
minSystem.Single
maxSystem.Single

Returns

System.String[]

public static float StarRating()

Generate a random star rating (1.0-5.0 with realistic distribution skewed toward 3.5-4.5).

Returns

System.Single

public static float[] StarRatings(int count)

Generate multiple random star ratings.

Parameters

NameType
countSystem.Int32

Returns

System.Single[]

public static string VersionNumber()

Generate a random semantic version number (e.g., “2.4.1”).

Returns

System.String

public static string[] VersionNumbers(int count)

Generate multiple random version numbers.

Parameters

NameType
countSystem.Int32

Returns

System.String[]

public static string FileName()

Generate a random file name with extension (e.g., “report_2024.pdf”).

Returns

System.String

public static string[] FileNames(int count)

Generate multiple random file names.

Parameters

NameType
countSystem.Int32

Returns

System.String[]

public static string UserAgent()

Generate a random browser user agent string.

Returns

System.String

public static string[] UserAgents(int count)

Generate multiple random user agent strings.

Parameters

NameType
countSystem.Int32

Returns

System.String[]

public static string Animal()

Generate a random animal name.

Returns

System.String

public static string[] Animals(int count)

Generate multiple random animal names.

Parameters

NameType
countSystem.Int32

Returns

System.String[]

public static string Hashtag()

Generate a random hashtag (e.g., “#gamedev”).

Returns

System.String

public static string[] Hashtags(int count)

Generate multiple random hashtags.

Parameters

NameType
countSystem.Int32

Returns

System.String[]

public static string Honorific()

Generate a random honorific (e.g., “Dr.”, “Mr.”).

Returns

System.String

public static string[] Honorifics(int count)

Generate multiple random honorifics.

Parameters

NameType
countSystem.Int32

Returns

System.String[]

public static string Emoji()

Generate a random emoji character.

Returns

System.String

public static string[] Emojis(int count)

Generate multiple random emojis.

Parameters

NameType
countSystem.Int32

Returns

System.String[]

public static T Enum<T>() where T : Enum

Generate a random enum value.

Returns

{T}

public static T[] Enums<T>(int count) where T : Enum

Generate multiple random enum values.

Parameters

NameType
countSystem.Int32

Returns

{T}[]

public static string Generate(SimpleDataType type)

Generate a single value of the specified type.

Parameters

NameType
typeMockMagic.SimpleDataType

Returns

System.String

public static string[] Generate(SimpleDataType type, int count)

Generate multiple values of the specified type.

Parameters

NameType
typeMockMagic.SimpleDataType
countSystem.Int32

Returns

System.String[]

public static void Seed(int seed)

Seed all MockMagic randomness for reproducible output. Equivalent to MockRandom.Seed(int).

Parameters

NameType
seedSystem.Int32
public static void ResetSeed()

Clear the seed and restore default behaviour.

public static T Pick<T>(params T[] items)

Pick a random element from an array.

Parameters

NameType
items{T}[]

Returns

{T}

PickWeighted(params (float weight, T value)[])

Section titled “PickWeighted(params (float weight, T value)[])”
public static T PickWeighted<T>(params (float weight, T value)[] items)

Pick a random element biased by weights.

Parameters

NameType
itemsSystem.ValueTuple{System.Single,{T}}[]

Returns

{T}

public static void Shuffle<T>(IList<T> items)

Shuffle a list in place.

Parameters

NameType
itemsSystem.Collections.Generic.IList{{T}}
public static T[] Subset<T>(T[] items, int count)

Return a random subset of unique items from an array.

Parameters

NameType
items{T}[]
countSystem.Int32

Returns

{T}[]

public static string ImageUrl(int width = 640, int height = 480)

Random picsum.photos URL for the requested dimensions.

Parameters

NameType
widthSystem.Int32
heightSystem.Int32

Returns

System.String

PlaceholderImageUrl(int, int, string, string, string)

Section titled “PlaceholderImageUrl(int, int, string, string, string)”
public static string PlaceholderImageUrl(int width = 640, int height = 480, string bg = null, string fg = null, string text = null)

placehold.co URL with optional background, foreground, and label.

Parameters

NameType
widthSystem.Int32
heightSystem.Int32
bgSystem.String
fgSystem.String
textSystem.String

Returns

System.String

public static string SvgDataUri(int width = 640, int height = 480, Color? fill = null, string label = null)

Self-contained SVG data URI — useful when you need an offline placeholder asset.

Parameters

NameType
widthSystem.Int32
heightSystem.Int32
fillSystem.Nullable{UnityEngine.Color}
labelSystem.String

Returns

System.String

public static IDisposable LocaleScope(string id)

Select data for a synchronous block, restoring its caller’s locale on dispose. Do not span await or yield.

Parameters

NameType
idSystem.String

Returns

System.IDisposable

public static IDisposable LocaleScope(MockLocale locale)

Selects a locale profile for the current synchronous scope and restores the previous locale on disposal.

Parameters

NameTypeDescription
localeMockMagic.MockLocaleThe non-null profile to use on the current thread.

Returns

System.IDisposable: A disposable scope for the selected locale.

Remarks

Do not retain the scope across an await or yield.

public static string Format(string pattern)

Replace symbols in pattern:

  • # → digit 0-9
  • ? → lowercase letter
  • * → digit or lowercase letter
  • \ → escape the next character (emit it literally)

Any other character is copied verbatim.

Parameters

NameType
patternSystem.String

Returns

System.String

Examples

Mock.Format(”###-##-####”) → “123-45-6789”

public static string Hash(int length = 40, bool upperCase = false)

Random hexadecimal string (default 40 chars == SHA-1 length).

Parameters

NameType
lengthSystem.Int32
upperCaseSystem.Boolean

Returns

System.String

public static string Hexadecimal(int length, bool upperCase = false)

Random hexadecimal string of the requested length.

Parameters

NameType
lengthSystem.Int32
upperCaseSystem.Boolean

Returns

System.String

public static string AlphaNumeric(int length)

Random lowercase alphanumeric string (0-9, a-z).

Parameters

NameType
lengthSystem.Int32

Returns

System.String

public static string Letters(int length, bool upperCase = false)

Random uppercase or mixed-case letter string of the requested length.

Parameters

NameType
lengthSystem.Int32
upperCaseSystem.Boolean

Returns

System.String

public static string ClampString(string value, int min, int max)

Truncate or pad value so its length falls in [min, max]. Padding uses lowercase letters.

Parameters

NameType
valueSystem.String
minSystem.Int32
maxSystem.Int32

Returns

System.String

public static string Review(int minWords = 8, int maxWords = 30)

Build a short product-style review. minWords and maxWords hint at the desired length — the result rarely strays outside the range. Remarks never repeat, so a very long minimum ends once every remark that fits the review’s tone is used.

Parameters

NameType
minWordsSystem.Int32
maxWordsSystem.Int32

Returns

System.String

public static MockLocale CurrentLocale { get; }

The locale selected by the current synchronous scope, or the compatibility Default.

Returns

MockMagic.MockLocale