Giter VIP home page Giter VIP logo

internalmock's Introduction

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")
          .Throws(exception);

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

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

      this.studentService.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: [email protected] Also join our Standard Community on Discord Community here:

Discord

internalmock's People

Contributors

hassanhabib avatar tehwardy avatar mabroukmahdhi avatar glhays avatar lboullosa avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.