TinyFakeHost 2.0.14

dotnet add package TinyFakeHost --version 2.0.14
                    
NuGet\Install-Package TinyFakeHost -Version 2.0.14
                    
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="TinyFakeHost" Version="2.0.14" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="TinyFakeHost" Version="2.0.14" />
                    
Directory.Packages.props
<PackageReference Include="TinyFakeHost" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add TinyFakeHost --version 2.0.14
                    
#r "nuget: TinyFakeHost, 2.0.14"
                    
#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.
#:package TinyFakeHost@2.0.14
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=TinyFakeHost&version=2.0.14
                    
Install as a Cake Addin
#tool nuget:?package=TinyFakeHost&version=2.0.14
                    
Install as a Cake Tool

TinyFakeHost

Build Status

Summary

TinyFakeHost is a library designed to simulate backend web service responses in your tests. It allows you to easily fake HTTP responses for your application, enabling more comprehensive testing compared to mocking the client method directly.

Why Use TinyFakeHost?

When testing applications that make backend calls, mocking client code can fall short. TinyFakeHost provides a simple but powerful solution to fake real HTTP backend behavior in your tests.

Use TinyFakeHost when you want to:

  • Simulate realistic backend responses, including query parameters, form posts, and HTTP methods.
  • Test for timeouts, slow responses, or specific HTTP status codes (e.g., 404, 500).
  • Assert that your application actually made the expected requests - not just return fake data.
  • View all incoming requests during tests for easier debugging.

TinyFakeHost helps you validate the request construction (including query parameters and method), timing issues, and other edge cases that are difficult to test using client-side mocks alone.

NuGet Package

https://www.nuget.org/packages/TinyFakeHost/

Port Conflict Handling

When running multiple fake hosts on the same port, TinyFakeHost automatically prevents conflicts by having one fake host wait until the other finishes. This eliminates the need for manual port management, making it safe to run tests in parallel.

Example Usage:

Start and Stop a Fake Host

[SetUp]
public void SetUp()
{
    _tinyFakeHost = new TinyFakeHost("http://localhost:5432/someService/v1/"); 
    _faker = _tinyFakeHost.GetFaker(); // Use this to fake backend responses
    _asserter = _tinyFakeHost.GetAsserter(); // Use this to assert expected queries
    _tinyFakeHost.Start();
}

[TearDown]
public void TearDown()
{
    _tinyFakeHost.Stop();
    _tinyFakeHost.Dispose();
}

Fake a Web Service Response

[Test]
public void Your_test_method()
{
    // your test code . . . .

    // Setup fake responses
    _faker.Fake(f => f
        .IfRequest("/vendors/9876-5432-1098-7654/products")
        .WithUrlParameters("type=desk&manufactureYear=2013")
        .ThenReturn(products)
    );

    _faker.Fake(f => f
        .IfRequest("/vendors")
        .ThenReturn(vendors)
    );

    _faker.Fake(f => f
        .IfRequest("/vendors/6543-2109-8765-4321/products")
		.WithMethod(Method.POST)
        .WithFormParameters("type=chair&manufactureYear=2014")
        .ThenReturn(result)
    );

    // your test code . . . .
}

Simulate Long Response Times (Timeout Testing)

[Test]
public void Your_test_method()
{
    // your test code . . . .

    // Setup a fake response with a delay (simulates a service timeout)
    _faker.Fake(f => f
        .IfRequest("/vendors")
        .ThenReturn(new FakeResponse{
            ContentType = "application/json",
            Content = @"{""message"":""Request Timeout""}",
            StatusCode = HttpStatusCode.RequestTimeout,
            MillisecondsSleep = 6000 // Simulate a timeout by sleeping for 6 seconds
        })
    );

    // your test code . . . .
}

Assert the Requested Query

[Test]
public void Your_test_method()
{
    // your test code . . . .

    // Assert that the expected request was made
    _asserter.Assert(a => a
        .Resource("/vendors/9876-5432-1098-7654/products")
        .WithUrlParameters("type=desk&manufactureYear=2013")
        .WasRequested()
    );

    _asserter.Assert(a => a
        .Resource("/vendors")
        .WasRequested()
    );

    _asserter.Assert(a => a
        .Resource("/vendors/6543-2109-8765-4321/products")
        .WithMethod(Method.POST)
        .WithFormParameters("type=chair&manufactureYear=2014")
        .WasRequested()
    );
}

Check All Requested Queries

private void PrintRequestedQueries()
{
    foreach (var requestedQuery in _tinyFakeHost.GetRequestedQueries())
        Console.WriteLine("Requested query - {0}", requestedQuery);
}

Alternatively, you can set the RequestedQueryPrint property to true to automatically print requested queries:

_tinyFakeHost.RequestedQueryPrint = true; // Automatically prints requested queries

Additional Notes

  • TinyFakeHost is best suited for integration testing, where you need to test the actual flow of requests and responses.
  • The fake host runs a real HTTP listener and responds to HTTP requests as configured, so you can simulate complex service behaviors, including failures, timeouts, and status codes.
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  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 was computed.  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 was computed.  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.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  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
2.0.14 205 5/11/2025
2.0.6 973 6/1/2020
2.0.0-alpha 737 3/19/2019
0.1.82-alpha 1,585 5/30/2016
0.1.70-alpha 1,109 5/28/2016
0.1.62 25,491 9/8/2015
0.1.0 2,228 3/16/2015
0.0.0.1 13,530 2/10/2015