Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
docs(mocks): document [GenerateMock] for static abstract member inter…
…faces

Explains the CS8920 workaround and the generated _Mockable bridge type.
  • Loading branch information
thomhurst committed Mar 30, 2026
commit 4d66d9902fe1cfde8f9b3982d768584e5a102faa
1 change: 1 addition & 0 deletions docs/docs/writing-tests/mocking/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ public class GreeterTests
| `Mock.OfDelegate<T>()` | Mock a delegate (`Func<>`, `Action<>`, etc.) |
| `Mock.Wrap<T>(instance)` | Wrap a real object with selective overrides |
| `Mock.Of<T1, T2>()` | Mock multiple interfaces on a single object |
| `[GenerateMock(typeof(T))]` | Generate a mock for interfaces with static abstract members ([details](setup#interfaces-with-static-abstract-members)) |
| `Mock.HttpHandler()` | Create a `MockHttpHandler` *(requires `TUnit.Mocks.Http`)* |
| `Mock.HttpClient(baseAddress?)` | Create a `MockHttpClient` — an `HttpClient` with a `.Handler` property *(requires `TUnit.Mocks.Http`)* |
| `Mock.Logger()` | Create a `MockLogger` *(requires `TUnit.Mocks.Logging`)* |
Expand Down
23 changes: 23 additions & 0 deletions docs/docs/writing-tests/mocking/setup.md
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,29 @@ Pass constructor arguments for non-default constructors:
var mock = Mock.Of<MyService>("connectionString", 42);
```

## Interfaces with Static Abstract Members

Interfaces that have static abstract members (directly or inherited) cannot be used as type arguments in `Mock.Of<T>()` — the compiler raises **CS8920** before the source generator runs.

Use `[assembly: GenerateMock(typeof(T))]` to work around this. The source generator produces a bridge interface (suffixed `_Mockable`) that provides default implementations for the static abstract members:

```csharp
using TUnit.Mocks;

[assembly: GenerateMock(typeof(IMyParseable))]

public interface IMyParseable : IParsable<IMyParseable>
{
string Format();
}

// In your test — use the generated bridge type:
var mock = Mock.Of<TUnit_Mocks_Tests_IMyParseable_Mockable>();
mock.Format().Returns("formatted");
```

The bridge type implements all the non-static members of the original interface, so you can set up and verify calls as normal.

## Delegate Mocking

Mock any delegate type:
Expand Down
Loading