Skip to content

Commit 6492d0c

Browse files
committed
Updated docs article on writing tests in .cs and .razor files
1 parent 8b88daf commit 6492d0c

33 files changed

Lines changed: 380 additions & 582 deletions

docs/samples/samples.sln

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "bunit.docs.mstest.samples",
1111
EndProject
1212
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "bunit.docs.nunit.samples", "tests\nunit\bunit.docs.nunit.samples.csproj", "{0BC8DBEB-500A-4DB2-9123-7617D24E5053}"
1313
EndProject
14-
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "bunit.docs.razor.samples", "tests\razor\bunit.docs.razor.samples.csproj", "{30518354-6437-4474-8198-870AA8B07A2D}"
15-
EndProject
1614
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "bunit.docs.xunit.samples", "tests\xunit\bunit.docs.xunit.samples.csproj", "{3B8B3B0E-CA33-4C76-901D-219F5FF848B3}"
1715
EndProject
1816
Global
@@ -61,18 +59,6 @@ Global
6159
{0BC8DBEB-500A-4DB2-9123-7617D24E5053}.Release|x64.Build.0 = Release|Any CPU
6260
{0BC8DBEB-500A-4DB2-9123-7617D24E5053}.Release|x86.ActiveCfg = Release|Any CPU
6361
{0BC8DBEB-500A-4DB2-9123-7617D24E5053}.Release|x86.Build.0 = Release|Any CPU
64-
{30518354-6437-4474-8198-870AA8B07A2D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
65-
{30518354-6437-4474-8198-870AA8B07A2D}.Debug|Any CPU.Build.0 = Debug|Any CPU
66-
{30518354-6437-4474-8198-870AA8B07A2D}.Debug|x64.ActiveCfg = Debug|Any CPU
67-
{30518354-6437-4474-8198-870AA8B07A2D}.Debug|x64.Build.0 = Debug|Any CPU
68-
{30518354-6437-4474-8198-870AA8B07A2D}.Debug|x86.ActiveCfg = Debug|Any CPU
69-
{30518354-6437-4474-8198-870AA8B07A2D}.Debug|x86.Build.0 = Debug|Any CPU
70-
{30518354-6437-4474-8198-870AA8B07A2D}.Release|Any CPU.ActiveCfg = Release|Any CPU
71-
{30518354-6437-4474-8198-870AA8B07A2D}.Release|Any CPU.Build.0 = Release|Any CPU
72-
{30518354-6437-4474-8198-870AA8B07A2D}.Release|x64.ActiveCfg = Release|Any CPU
73-
{30518354-6437-4474-8198-870AA8B07A2D}.Release|x64.Build.0 = Release|Any CPU
74-
{30518354-6437-4474-8198-870AA8B07A2D}.Release|x86.ActiveCfg = Release|Any CPU
75-
{30518354-6437-4474-8198-870AA8B07A2D}.Release|x86.Build.0 = Release|Any CPU
7662
{3B8B3B0E-CA33-4C76-901D-219F5FF848B3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
7763
{3B8B3B0E-CA33-4C76-901D-219F5FF848B3}.Debug|Any CPU.Build.0 = Debug|Any CPU
7864
{3B8B3B0E-CA33-4C76-901D-219F5FF848B3}.Debug|x64.ActiveCfg = Debug|Any CPU
@@ -92,7 +78,6 @@ Global
9278
GlobalSection(NestedProjects) = preSolution
9379
{C2060A20-8C56-4678-B65C-DCF52D70223D} = {FC897929-3A4D-4276-9AF4-C77B0FAF221E}
9480
{0BC8DBEB-500A-4DB2-9123-7617D24E5053} = {FC897929-3A4D-4276-9AF4-C77B0FAF221E}
95-
{30518354-6437-4474-8198-870AA8B07A2D} = {FC897929-3A4D-4276-9AF4-C77B0FAF221E}
9681
{3B8B3B0E-CA33-4C76-901D-219F5FF848B3} = {FC897929-3A4D-4276-9AF4-C77B0FAF221E}
9782
EndGlobalSection
9883
GlobalSection(ExtensibilityGlobals) = postSolution
Lines changed: 6 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,13 @@
1-
using System;
2-
using Bunit;
3-
using Bunit.Rendering;
4-
using Microsoft.AspNetCore.Components;
51
using Microsoft.VisualStudio.TestTools.UnitTesting;
62

73
namespace Bunit.Docs.Samples
84
{
9-
public abstract class BunitTestContext : IDisposable
10-
{
11-
private Bunit.TestContext _context;
12-
13-
public ITestRenderer Renderer => _context?.Renderer ?? throw new InvalidOperationException("MSTest has not started executing tests yet");
14-
15-
public TestServiceProvider Services => _context?.Services ?? throw new InvalidOperationException("MSTest has not started executing tests yet");
16-
17-
public void Dispose()
5+
public abstract class BunitTestContext : TestContextWrapper
186
{
19-
_context?.Dispose();
20-
_context = null;
21-
}
7+
[TestInitialize]
8+
public void Setup() => TestContext = new Bunit.TestContext();
229

23-
[TestInitialize]
24-
public void Setup() => _context = new Bunit.TestContext();
25-
26-
[TestCleanup]
27-
public void TearDown() => Dispose();
28-
29-
public IRenderedComponent<TComponent> RenderComponent<TComponent>(params ComponentParameter[] parameters) where TComponent : IComponent
30-
=> _context?.RenderComponent<TComponent>(parameters) ?? throw new InvalidOperationException("MSTest has not started executing tests yet");
31-
32-
public IRenderedComponent<TComponent> RenderComponent<TComponent>(Action<ComponentParameterCollectionBuilder<TComponent>> parameterBuilder) where TComponent : IComponent
33-
=> _context?.RenderComponent<TComponent>(parameterBuilder) ?? throw new InvalidOperationException("MSTest has not started executing tests yet");
34-
}
10+
[TestCleanup]
11+
public void TearDown() => TestContext?.Dispose();
12+
}
3513
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
@attribute [TestClass]
2+
@inherits BunitTestContext
3+
@code
4+
{
5+
[TestMethod]
6+
public void HelloWorldComponentRendersCorrectly()
7+
{
8+
// Act
9+
var cut = Render(@<HelloWorld/>);
10+
11+
// Assert
12+
cut.MarkupMatches(@<h1>Hello world from Blazor</h1>);
13+
}
14+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
@attribute [TestClass]
2+
@code
3+
{
4+
[TestMethod]
5+
public void HelloWorldComponentRendersCorrectly()
6+
{
7+
// Arrange
8+
using var ctx = new Bunit.TestContext();
9+
10+
// Act
11+
var cut = ctx.Render(@<HelloWorld/>);
12+
13+
// Assert
14+
cut.MarkupMatches(@<h1>Hello world from Blazor</h1>);
15+
}
16+
}
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
1-
@namespace Bunit.Docs.Samples
2-
@using global::Xunit
3-
41
@using Microsoft.AspNetCore.Components.Forms
5-
@using Microsoft.AspNetCore.Components.Routing
62
@using Microsoft.AspNetCore.Components.Web
73
@using Microsoft.JSInterop
84
@using Microsoft.Extensions.DependencyInjection
95
@using AngleSharp.Dom
106
@using Bunit
117
@using Bunit.TestDoubles
12-
@using Xunit
8+
@using Microsoft.VisualStudio.TestTools.UnitTesting
Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,14 @@
1-
using System;
21
using Bunit;
3-
using Bunit.Rendering;
4-
using Microsoft.AspNetCore.Components;
52
using NUnit.Framework;
63

74
namespace Bunit.Docs.Samples
85
{
9-
public abstract class BunitTestContext : IDisposable
10-
{
11-
private Bunit.TestContext _context;
12-
13-
public ITestRenderer Renderer => _context?.Renderer ?? throw new InvalidOperationException("NUnit has not started executing tests yet");
14-
15-
public TestServiceProvider Services => _context?.Services ?? throw new InvalidOperationException("NUnit has not started executing tests yet");
16-
17-
public void Dispose()
6+
public abstract class BunitTestContext : TestContextWrapper
187
{
19-
_context?.Dispose();
20-
_context = null;
21-
}
22-
238
[SetUp]
24-
public void Setup() => _context = new Bunit.TestContext();
9+
public void Setup() => TestContext = new Bunit.TestContext();
2510

2611
[TearDown]
27-
public void TearDown() => Dispose();
28-
29-
public IRenderedComponent<TComponent> RenderComponent<TComponent>(params ComponentParameter[] parameters) where TComponent : IComponent
30-
=> _context?.RenderComponent<TComponent>(parameters) ?? throw new InvalidOperationException("NUnit has not started executing tests yet");
31-
32-
public IRenderedComponent<TComponent> RenderComponent<TComponent>(Action<ComponentParameterCollectionBuilder<TComponent>> parameterBuilder) where TComponent : IComponent
33-
=> _context?.RenderComponent<TComponent>(parameterBuilder) ?? throw new InvalidOperationException("NUnit has not started executing tests yet");
12+
public void TearDown() => TestContext?.Dispose();
3413
}
3514
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
@inherits BunitTestContext
2+
@code
3+
{
4+
[Test]
5+
public void HelloWorldComponentRendersCorrectly()
6+
{
7+
// Act
8+
var cut = Render(@<HelloWorld />);
9+
10+
// Assert
11+
cut.MarkupMatches(@<h1>Hello world from Blazor</h1>);
12+
}
13+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
@code
2+
{
3+
[Test]
4+
public void HelloWorldComponentRendersCorrectly()
5+
{
6+
// Arrange
7+
using var ctx = new Bunit.TestContext();
8+
9+
// Act
10+
var cut = ctx.Render(@<HelloWorld />);
11+
12+
// Assert
13+
cut.MarkupMatches(@<h1>Hello world from Blazor</h1>);
14+
}
15+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
@using Microsoft.AspNetCore.Components.Forms
2+
@using Microsoft.AspNetCore.Components.Web
3+
@using Microsoft.JSInterop
4+
@using Microsoft.Extensions.DependencyInjection
5+
@using AngleSharp.Dom
6+
@using Bunit
7+
@using Bunit.TestDoubles
8+
@using NUnit.Framework

docs/samples/tests/razor/AllFixtureParameters.razor

Lines changed: 0 additions & 20 deletions
This file was deleted.

0 commit comments

Comments
 (0)