Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
482 changes: 482 additions & 0 deletions docs/docs/migration/mocking/fakeiteasy.md

Large diffs are not rendered by default.

502 changes: 502 additions & 0 deletions docs/docs/migration/mocking/moq.md

Large diffs are not rendered by default.

484 changes: 484 additions & 0 deletions docs/docs/migration/mocking/nsubstitute.md

Large diffs are not rendered by default.

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 @@ -210,3 +210,4 @@ See [Argument Matchers](argument-matchers) for the full API.
- [Advanced Features](advanced) — state machines, events, auto-mocking, diagnostics, and more
- [HTTP Mocking](http) — mock `HttpClient` with `MockHttpHandler`
- [Logging](logging) — capture and verify `ILogger` calls with `MockLogger`
- Migrating from another library — [Moq](../../migration/mocking/moq.md), [NSubstitute](../../migration/mocking/nsubstitute.md), or [FakeItEasy](../../migration/mocking/fakeiteasy.md)
10 changes: 10 additions & 0 deletions docs/sidebars.ts
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,16 @@ const sidebars: SidebarsConfig = {
'migration/xunit',
'migration/nunit',
'migration/mstest',
{
type: 'category',
label: 'Mocking Libraries',
collapsed: true,
items: [
'migration/mocking/moq',
'migration/mocking/nsubstitute',
'migration/mocking/fakeiteasy',
],
},
],
},
{
Expand Down
3 changes: 2 additions & 1 deletion skills/tunit/SKILL.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
name: tunit
description: Write or troubleshoot tests using TUnit, TUnit.Assertions, or TUnit.Mocks, or migrate tests to TUnit. Routes to task-specific official documentation. Use only for tasks involving these packages or migration to TUnit.
description: Write or troubleshoot tests using TUnit, TUnit.Assertions, or TUnit.Mocks, or migrate tests or mocks to TUnit or TUnit.Mocks. Routes to task-specific official documentation. Use only for tasks involving these packages or migration to them.
license: MIT
---

Expand Down Expand Up @@ -32,6 +32,7 @@ Choose the link that matches the task; these are alternatives, not a reading che
| Run tests or select tests | [Running tests](https://tunit.dev/docs/getting-started/running-your-tests.md) or [filters](https://tunit.dev/docs/execution/test-filters.md) |
| Migrate an existing suite | [xUnit](https://tunit.dev/docs/migration/xunit.md), [NUnit](https://tunit.dev/docs/migration/nunit.md), or [MSTest](https://tunit.dev/docs/migration/mstest.md), matching the source framework |
| Mocking | [TUnit.Mocks](https://tunit.dev/docs/writing-tests/mocking.md) |
| Migrate mocks to TUnit.Mocks | [Moq](https://tunit.dev/docs/migration/mocking/moq.md), [NSubstitute](https://tunit.dev/docs/migration/mocking/nsubstitute.md), or [FakeItEasy](https://tunit.dev/docs/migration/mocking/fakeiteasy.md), matching the source library |
| Aspire integration testing | [Aspire](https://tunit.dev/docs/examples/aspire.md) |
| ASP.NET Core integration testing | [ASP.NET Core](https://tunit.dev/docs/examples/aspnet.md) |
| Playwright browser testing | [Playwright](https://tunit.dev/docs/examples/playwright.md) |
Expand Down
2 changes: 2 additions & 0 deletions tests/TUnit.DocTests/TUnit.DocTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,15 @@
<PackageReference Include="BenchmarkDotNet" />
<PackageReference Include="bunit" />
<PackageReference Include="CliWrap" />
<PackageReference Include="FakeItEasy" />
<PackageReference Include="FsCheck" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" />
<PackageReference Include="Microsoft.EntityFrameworkCore" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" />
<PackageReference Include="Microsoft.Extensions.Logging" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" />
<PackageReference Include="Microsoft.Playwright" />
<PackageReference Include="Moq" />
<PackageReference Include="MSTest.TestFramework" />
<PackageReference Include="NSubstitute" />
<PackageReference Include="NUnit" />
Expand Down
32 changes: 32 additions & 0 deletions tools/TUnit.DocSnippetGenerator/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,7 @@ static string GenerateSource(
else
{
builder.AppendLine($"namespace {generatedNamespace};");
AppendMockingLibraryUsings(builder, snippet);
}

builder.AppendLine($"#line {snippet.Line} \"{snippet.SourcePath}\"")
Expand Down Expand Up @@ -504,6 +505,7 @@ static string GenerateSource(
}

builder.AppendLine($"namespace {generatedNamespace};");
AppendMockingLibraryUsings(builder, snippet);

if (containsExtensionMethod)
{
Expand Down Expand Up @@ -691,6 +693,36 @@ static void AppendFrameworkAliases(StringBuilder builder, Snippet snippet)
}
}

// Mocking-library migration pages compare Moq, NSubstitute, and FakeItEasy with TUnit.Mocks.
// Their type names (Mock<T>, Times, MockBehavior, Arg) clash with TUnit.Mocks' global usings,
// so a fence marked with the library's name imports it inside the namespace, where lookup
// finds it before the global usings.
static void AppendMockingLibraryUsings(StringBuilder builder, Snippet snippet)
{
if (!snippet.SourcePath.Contains("/migration/mocking/", StringComparison.OrdinalIgnoreCase))
{
return;
}

var marker = Regex.Match(snippet.Source, @"(?m)^\s*//\s*(Moq|NSubstitute|FakeItEasy)\s*$");
if (!marker.Success)
{
return;
}

var namespaces = marker.Groups[1].Value switch
{
"Moq" => new[] { "Moq" },
"NSubstitute" => new[] { "NSubstitute", "NSubstitute.ExceptionExtensions", "NSubstitute.ReceivedExtensions" },
_ => new[] { "FakeItEasy" }
};

foreach (var ns in namespaces)
{
builder.AppendLine($"using {ns};");
}
}

static string QualifyLocallyDeclaredAttributeTypes(string attribute, string source, string generatedNamespace)
{
var declaredTypes = Regex.Matches(
Expand Down
Loading