Skip to content

Key API Reference

A compact list of the core types and fluent members you’ll use most. This is not a full API surface — just the names and signatures you need to get productive.

Attribute

  • GenerateImposterAttribute
  • Usage: [assembly: Imposter.Abstractions.GenerateImposter(typeof(TTarget), bool putInTheSameNamespace = true)]
  • Parameters:
    • Type type — target interface or class
    • bool putInTheSameNamespace = true - try to place the generated imposter in the target's namespace

Pro tip

Add the attribute at the assembly level in a single file to keep generation centralized and easy to audit.

Argument Matchers

  • Arg
  • static AnyArgMarker Any — untyped wildcard; implicitly converts to Arg<T>.Any() for each parameter type

  • Arg<T>

  • static Arg<T> Is(Func<T, bool> predicate)
  • static Arg<T> Is(T? value)
  • static Arg<T> Is(T? value, IEqualityComparer<T> comparer)
  • static Arg<T> IsNot(Func<T, bool> predicate)
  • static Arg<T> IsNot(T? value)
  • static Arg<T> IsNot(T? value, IEqualityComparer<T> comparer)
  • static Arg<T> IsIn(IEnumerable<T> values)
  • static Arg<T> IsIn(IEnumerable<T> values, IEqualityComparer<T> comparer)
  • static Arg<T> IsNotIn(IEnumerable<T> values)
  • static Arg<T> IsNotIn(IEnumerable<T> values, IEqualityComparer<T> comparer)
  • static Arg<T> IsDefault()
  • static Arg<T> Any()
  • static implicit operator Arg<T>(T value)
  • static implicit operator Arg<T>(AnyArgMarker _) — enables Arg.Any shorthand

  • OutArg<T>

  • static OutArg<T> Any() — wildcard for out parameters

  • SpanArg<T> — matches the elements of a Span<T> parameter

  • static SpanArg<T> Any()
  • static SpanArg<T> Is(params T[] expected)
  • static SpanArg<T> Is(T[] expected, IEqualityComparer<T> comparer)
  • static SpanArg<T> Is(Func<T[], bool> predicate)
  • static implicit operator SpanArg<T>(AnyArgMarker _) — enables Arg.Any shorthand

  • ReadOnlySpanArg<T> — matches the elements of a ReadOnlySpan<T> parameter, with the same members as SpanArg<T>

  • OutSpanArg<T> / OutReadOnlySpanArg<T>

  • static Any() — wildcard for out Span<T> / out ReadOnlySpan<T> parameters

Verification Counts

  • Count
  • static Count Exactly(int count)
  • static Count AtLeast(int count)
  • static Count AtMost(int count)
  • static Count Never()
  • static Count Once()
  • static readonly Count Any

Imposter Mode

  • ImposterMode
  • Implicit — missing setups return defaults (loose)
  • Explicit — missing setups throw MissingImposterException (strict)

Imposter Instance

  • IHaveImposterInstance<TInstance>
  • Every generated imposter implements it; Instance is the target-typed object to hand to the code under test
  • Use it as a generic constraint when a test helper takes any imposter of a target: where TImposter : IHaveImposterInstance<TTarget>
  • ImposterExtensions
  • Instance<TInstance>(this IHaveImposterInstance<TInstance> imposter) — the usual call site: imposter.Instance()

Note

TypeCaster, SpanElementsComparer<T> and SetupCollection<T> are also public, but only because generated imposters call them. They are hidden from IntelliSense and may change with the generator in the same package.

Method Setups (generated)

  • Typical fluent members on a method setup builder:
  • Returns(TResult value) — for a method returning Span<T> or ReadOnlySpan<T>, Returns(T[] value): each call returns a span over the array
  • Returns(Func<TResult> factory)
  • Returns(Func<...parameters..., TResult> factory) — delegate receives in/ref/out/in params
  • ReturnsAsync(TResult value)
  • ReturnsAsync(Func<Task<TResult>> factory)
  • Throws(Exception ex)
  • Throws<TException>()
  • Callback(Action action) / Callback(Action<...parameters...> action)
  • Then() — chain next behavior (return/throw/etc.) for subsequent matching calls
  • Called(Count count) — verify invocation frequency
  • A method whose type parameter allows ref structs: the setup takes matchers for the other parameters only, as in imposter.Measure<Gauge>(Arg<int>.Any()), and applies to calls with the same type arguments only (see Type parameters that allow ref structs)

Property Setups (generated)

  • Getter: imposter.Property.Getter() → supports Returns(...), Throws(...), Callback(...), Then(), Called(Count)
  • Setter: imposter.Property.Setter(Arg<T> valueMatcher) → supports Callback(...), Called(Count)
  • A property of another ref struct type: the getter's Returns takes only a delegate, and Setter() takes no matcher (see Ref struct properties)
  • Base implementation: UseBaseImplementation() (when applicable for classes)

Pro tip

Do not edit generated .g.cs files. Change behavior via setups or generator inputs, then rebuild.

Indexer Setups (generated)

  • Indexer access: imposter[Arg<T1>..., Arg<TN>...]
  • Getter: .Getter() → Returns(...), Throws(...), Callback(...), Then(), Called(Count)
  • Setter: .Setter() / .Setter(Arg<TValue> valueMatcher) → Callback(...), Called(Count)
  • Base implementation: UseBaseImplementation()
  • An indexer of another ref struct type: the getter's Returns takes only the delegate that gets the keys (see Ref struct values)
  • A key of another ref struct type takes no Arg<T>; an indexer with no other keys, or whose other keys are another indexer's, is set up by a method such as imposter.Indexer() (see Ref struct keys)

Event Helpers (generated)

  • Raise: imposter.Event.Raise(object? sender, EventArgs args) (or concrete event args type)
  • Verify:
  • imposter.Event.Subscribed(Arg<Delegate>.Is(handler), Count count)
  • imposter.Event.HandlerInvoked(Arg<Delegate>.Is(handler), Count count)

See also: Cheat Sheet and Limitations.