InternalMock 0.2.0

dotnet add package InternalMock --version 0.2.0                
NuGet\Install-Package InternalMock -Version 0.2.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="InternalMock" Version="0.2.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add InternalMock --version 0.2.0                
#r "nuget: InternalMock, 0.2.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 InternalMock as a Cake Addin
#addin nuget:?package=InternalMock&version=0.2.0

// Install InternalMock as a Cake Tool
#tool nuget:?package=InternalMock&version=0.2.0                

<p align="center"> <img width="25%" height="25%" src="https://github.com/hassanhabib/InternalMock/blob/master/InternalMock/InternalMock.png"> </p>

InternalMock

.Net The Standard - COMPLIANT

Introduction

This is a beta release please use carefully.

This library allows you to mock internal private static methods in your service - please watch the following videos for context:

Architecture

Here's the architecture of the library according to The Standard:

image

Examples

Let's Assume you have a service that has several functions that don't call any dependencies. Your service is what we call self-sufficient or dead-end service, as the flow stops there and might just be returned from the same service. An example of a service like this is a tax calculation service, you pass the total income, along with some other details and it calculates the taxes for a certain year. It doesn't call any dependencies.

Now, in that very unique scenario we need to find a way to test-drive that our self-sufficient service here can handle a generic exception or any other exception of any type. Since there are no dependencies injected, it's impossible to tag an exception the regular way where we do:

  this.someDependency.Setup(dependency =>
    dependency.GetStuff())
      .Throws(exception);

The solution here is to create inner dependencies. A self-sufficient service can rely on multiple other private static functions to perform certain functions. We can mock these functions without changing the exception handling code as follows:

Let's say our service looks like this:

public string RetrieveStudentFullName(string firstName, string lastName)
{
  ValidateStudentName(firstName, lastName);

  return $"{firstName} {lastName}";
}

We can write the test as follows to make the ValidateStudentName function throw an exception as follows:

  [Fact]
  public void ShouldThrowServiceExceptionOnRetrieveStudentFullNameIfServiceErrorOccurrs3()
  {
      // given
      var exception = new Exception();

      this.studentService.Mock(
        methodName: "ValidateStudentName",
        exception: exception);

      // when
      Action retrieveStudentFullNameAction = () =>
        this.studentService.RetrieveStudentFullName(
          firstName: "Hassan", 
          lastName: "Habib");

      // then
      Assert.Throws<StudentServiceException>(retrieveStudentFullNameAction);

      this.expressionService.ClearAllOtherCalls();
  }

And now we can make that very same test pass by doing the following:

  public string RetrieveStudentFullName(string firstName, string lastName)
  {
    try
    {
      ValidateStudentName(firstName, lastName);

      return $"{firstName} {lastName}";
    }
    catch (Exception exception)
    {
      throw new StudentServiceException(exception);
    }
  }

Contact us

For more information contact Hassan Habib: hassanhabib@live.com Also join our Standard Community on Discord Community here:

Discord

Product Compatible and additional computed target framework versions.
.NET 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 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. 
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
0.2.0 768 4/24/2022
0.1.0 430 4/24/2022

This is a beta release