Getting Started
What is Skugga?
Skugga (Swedish for Shadow) is a mocking library engineered specifically for Native AOT and Cloud-Native .NET. It moves mocking logic from runtime reflection to compile-time code generation, making it 100% AOT-compatible with zero overhead.
Legacy tools like Moq rely on System.Reflection.Emit to generate proxy objects at runtime. Since Native AOT strips away the JIT compiler, these tools crash instantly. Skugga eliminates this trade-off by generating mock implementations during the build process.
Installation
1. Install Skugga
dotnet add package SkuggaRequirements
- .NET 8.0 or later
- C# 12 enabled (LangVersion
12orlatest)
2. Enable Interceptors
Add the following to your test project's .csproj:
<PropertyGroup>
<InterceptorsPreviewNamespaces>$(InterceptorsPreviewNamespaces);Skugga</InterceptorsPreviewNamespaces>
</PropertyGroup>Writing Your First Test
using Skugga.Core;
public interface IEmailService
{
string GetEmailAddress(int userId);
string TenantName { get; }
}
public class MyFirstTest
{
public void Run()
{
// 1. Create the mock (intercepted at compile time)
var mock = Mock.Create<IEmailService>();
// 2. Configure behavior
mock.Setup(x => x.GetEmailAddress(1)).Returns("digvijay@digvijay.dev");
mock.Setup(x => x.TenantName).Returns("Contoso");
// 3. Execute
var email = mock.GetEmailAddress(1); // Returns "digvijay@digvijay.dev"
var tenant = mock.TenantName; // Returns "Contoso"
}
}How It Works
Skugga leverages C# 12 Interceptors to seamlessly rewire your code during compilation:
- Scan -- The Source Generator detects calls to
Mock.Create<T>(). - Generate -- It writes a concrete, optimized C# class (
Skugga_T) that implementsT. - Intercept -- The compiler replaces your
Mock.Createcall withnew Skugga_T().
Zero Friction: To the developer, it looks like a normal method call. To the runtime, it looks like hand-written, optimized code.
Next Steps
- Mock Creation -- Explore all ways to create mocks
- Setup & Returns -- Configure mock behavior
- Verification -- Assert method calls happened
- Migrating from Moq -- Seamless migration guide
- Architecture -- Deep dive into how Skugga works