Table of Contents

Class StaticOverride

Namespace
Valheim.Testing
Assembly
Valheim.Testing.dll

Sets static fields, static properties and environment variables for one test and puts back the values they had before, not a default the test assumes:

using var plain = StaticOverride.Set(() => MyMod.Meander, 0f).And(() => MyMod.Enabled, false).AndEnvironment("MYMOD_DEBUG", "1");

Dispose restores in reverse order, restores every entry even when one restore throws, and does nothing the second time. If a step of the chain throws (a readonly field, say), what the chain had already set is restored before the error reaches the caller, since its using statement never ran. Keep<T>(Expression<Func<T>>) records a value without changing it, for statics the code under test changes itself. Statics and environment variables are process-wide, so tests that override them must not run in parallel with tests that read them.

public sealed class StaticOverride
Inheritance
object
StaticOverride

Methods

AndEnvironment(string, string?)

public StaticOverride AndEnvironment(string name, string? value)

Parameters

name string
value string

Returns

StaticOverride

AndKeep<T>(Expression<Func<T>>)

public StaticOverride AndKeep<T>(Expression<Func<T>> member)

Parameters

member Expression<Func<T>>

Returns

StaticOverride

Type Parameters

T

And<T>(Expression<Func<T>>, T)

public StaticOverride And<T>(Expression<Func<T>> member, T value)

Parameters

member Expression<Func<T>>
value T

Returns

StaticOverride

Type Parameters

T

Dispose()

public void Dispose()

Environment(string, string?)

Sets an environment variable (null removes it) until disposed.

public static StaticOverride Environment(string name, string? value)

Parameters

name string
value string

Returns

StaticOverride

Keep<T>(Expression<Func<T>>)

Records member and restores it on dispose, whatever the test or the code under test sets.

public static StaticOverride Keep<T>(Expression<Func<T>> member)

Parameters

member Expression<Func<T>>

Returns

StaticOverride

Type Parameters

T

Set<T>(Expression<Func<T>>, T)

Sets member (() => Type.Member) to value until disposed.

public static StaticOverride Set<T>(Expression<Func<T>> member, T value)

Parameters

member Expression<Func<T>>
value T

Returns

StaticOverride

Type Parameters

T