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 classbool 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 toArg<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 _)— enablesArg.Anyshorthand -
OutArg<T> -
static OutArg<T> Any()— wildcard foroutparameters -
SpanArg<T>— matches the elements of aSpan<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 _)— enablesArg.Anyshorthand -
ReadOnlySpanArg<T>— matches the elements of aReadOnlySpan<T>parameter, with the same members asSpanArg<T> -
OutSpanArg<T>/OutReadOnlySpanArg<T> static Any()— wildcard forout Span<T>/out ReadOnlySpan<T>parameters
Verification Counts¶
Countstatic 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¶
ImposterModeImplicit— missing setups return defaults (loose)Explicit— missing setups throwMissingImposterException(strict)
Imposter Instance¶
IHaveImposterInstance<TInstance>- Every generated imposter implements it;
Instanceis 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> ImposterExtensionsInstance<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 returningSpan<T>orReadOnlySpan<T>,Returns(T[] value): each call returns a span over the arrayReturns(Func<TResult> factory)Returns(Func<...parameters..., TResult> factory)— delegate receives in/ref/out/in paramsReturnsAsync(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 callsCalled(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()→ supportsReturns(...),Throws(...),Callback(...),Then(),Called(Count) - Setter:
imposter.Property.Setter(Arg<T> valueMatcher)→ supportsCallback(...),Called(Count) - A property of another
ref structtype: the getter'sReturnstakes only a delegate, andSetter()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 structtype: the getter'sReturnstakes only the delegate that gets the keys (see Ref struct values) - A key of another
ref structtype takes noArg<T>; an indexer with no other keys, or whose other keys are another indexer's, is set up by a method such asimposter.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.