GwtUnit.XUnit 1.11.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package GwtUnit.XUnit --version 1.11.0                
NuGet\Install-Package GwtUnit.XUnit -Version 1.11.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="GwtUnit.XUnit" Version="1.11.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add GwtUnit.XUnit --version 1.11.0                
#r "nuget: GwtUnit.XUnit, 1.11.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install GwtUnit.XUnit as a Cake Addin
#addin nuget:?package=GwtUnit.XUnit&version=1.11.0

// Install GwtUnit.XUnit as a Cake Tool
#tool nuget:?package=GwtUnit.XUnit&version=1.11.0                

GWT Unit - Given When Then unit testing.

This library provides a base classes for unit testing. Using a DSL called Given When Then to facilitate and structure tests for readability and TDD.

The difference of base classes resides with the Given property. One class uses a dynamic type and the other uses a class you specify. In both cases the Thens class is specified using generic syntax.
The dynamic typing allows properties to be created on the the fly without having previously defined it. The only downside is lack of intellisense in VisualStduio(c).

GWT derives from AAA (Arrange Act Assert) but strives to provide organization and readability in the tests.

Given (Arrange)

Setup the data and context for the test.

When (Act)

Make the When actions composable. The When method takes params array of Action methods.

Then (Assert)

A class where all result context can be stored during a test and can be asserted on. I prefer using FluentAssertions from NuGet. These extension methods provide cleaner test failure messages and make the code more readable.

Features
  • Exception assertions closer to the origin of the thrown exception
  • Given and Then automatically cleaned up before each test
  • When assumes the Creating action will be done first and can be ommitted, however if Creating is provided then it will not be called automatically.
  • XUnitTestBase is purpose built to accomodate the XUnit framework.
  • Use Dependency Injection through IServiceCollection and IServiceProvider.
XUnit support
using FluentAssertions;
using Randal.Core.Testing.XUnit;

namespace Someplace
{
	public sealed class TestObjectTests : XUnitTestBase<TestObjectTests.Thens>
	{		
		[Fact, PositiveTest]
		public void ShouldHaveValidInstanceWithValue_WhenCreatingObject_GivenValue123()
		{
			Given.NeededValue = 123;	// Given is a dynamic object, create any number of property values on the fly
			
			When(Creating);				// 'When' consumes and executes a list of Action
			
			Then.Target.Should().NotBeNull();
		}
		
		[Fact, PositiveTest]
		public void ShouldHaveFormattedText_WhenFormatting_GivenInstanceWithValue123()
		{
			Given.NeededValue = 123;
			
			When(Formatting);	// Creating can be left out, as it is assumed as our first action
			
			Then.Text.Should().Be("Object said, 123");
		}
		
		[Fact, NegativeTest]
		public void ShouldThrowFormatExcpetion_WhenFormatting_GivenUnescapedOpeningBrace()
		{
			Given.Text = "Hey {name,";

			When(Defer(Formatting));

			DeferredAction.Should().Throw<FormatException>("Oops");
		}
		
		[Fact, PositiveTest]
		public void ShouldRepeatAction_WhenRepeatIncrementing()
		{
			When(Repeat(Incrementing, 10));

			Then.Repetitions.Should().Be(10);
		}

		[Fact, PositiveTest]
		public void ShouldAwaitAsynchronousFunction_WhenTestingAsyncMethod()
		{
			When(Await(Processing));

			Then.DelayedValue.Should().Be(4567);
		}

		protected override Creating()
		{
			// can check if a dynamic value is defined through  GivensDefined("NeededValue",...)

			Then.Target = new TestObject(Given.NeededValue);
		}
		
		private void Formatting()
		{
			Then.Text = Then.Target.Format();
		}

		private void Incrementing()
		{
			Then.Repetitions++;
		}

		private async Task Processing()
		{
			await Task.Delay(1000);

			Then.DelayedValue = 4567;
		}

		public sealed class Thens : IDisposable // optionally define as IDisposable to have automatic disposal after each test
		{
			public TestObject Target;
			public string Text;
			public int Repetitions;
			public int DelayedValue;

			public void Dispose()
			{
				// optionally define as IDisposable to have automatic disposal after each test
			}
		}
	}
}

Make use of dependency injection and helper methods

public class MyTest : XUnitTestBase<MyTest.Thens>
{
    [Fact]
    public void ShouldHaveMockEngaged_WhenTakingAction()
    {
        When(TakingAction);
        
        RequireMock<IDidSomething>().Verify(x => x.CallMe());
        Require<IDidSomething>().Should().NotBeNull();
    }
		
    protected override void Creating()
    {
        Services.AddScoped<A>();
        CreateMock<IDidSomething>(mock =>
        {
            if (TryGiven("ThrowException", out bool throwEx))
                mock.Setup(x => x.CallMe()).Throws<InvalidOperationException>();
        });

        Then.Target = BuildTarget<B>();
    }
}
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 is compatible.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.1 is compatible. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.11.1 135 12/5/2024
1.11.0 84 12/5/2024
1.10.2 84 12/5/2024
1.10.1 82 12/5/2024
1.10.0 83 12/5/2024
1.9.0 762 3/27/2024
1.8.0 159 3/13/2024
1.7.0 654 6/13/2022
1.6.0 437 6/4/2022
1.5.3 552 3/15/2022
1.5.2 397 3/15/2022
1.5.1 467 3/1/2022
1.5.0 414 3/1/2022
1.4.0 447 2/18/2022
1.3.0 424 2/18/2022
1.2.0 421 2/10/2022
1.1.0 416 2/9/2022
1.0.0 434 2/9/2022