Unit Test - Testing API Methods



  • I was reading the following article

    Unit testing an API

    This article is about unit testing an API and the methods on a class through the interface. What I do not seem to grasp is the fact that in my idea you would want to test the method implementation / functionality of the 'real' methods?

    So If I implement the method 'GetById', I would want to test the functionality of that function. However the Article shows how to create a Fake and test the Fake implementation of 'GetById'. This way you have two different definitions and testing just a 'totally different method / piece of code' and not the implementation / code you would like to see tested in the first place?

    Maybe I am missing the point of unit tests here?



  • So If I implement the method 'GetById', I would want to test the functionality of that function.

    Sure, and these tests would go in a ShoppingCartServiceTest class.

    However the Article shows how to create a Fake and test the Fake implementation of 'GetById'.

    Observe the test class of the article is ShoppingCartControllerTest. It is testing the controller, the class contains the endpoints:

    // DELETE api/shoppingcart/5
    [HttpDelete("{id}")]
    public ActionResult Remove(Guid id)
    

    And this class depends on a service (which fetches the data itself):

    var existingItem = _service.GetById(id);
    

    ShoppingCartControllerTest class is not worried about testing if the fetching is correct, but that the Controller calls the service in the correct moments and performs the necessary actions:

    // Code this class wants to test
    var existingItem = _service.GetById(id);
    
    if (existingItem == null)
    {
        return NotFound();
    }
    
     _service.Remove(id);
     return Ok();
    

    For this reason, one should create a Fake of the service, putting in control of the test the behavior of the service and allowing test isolation to the behavior of the Controller. The Controller class is designed in order to get the service injected - and this service can be a real implementation or a fake.

    public ShoppingCartControllerTest()
     {
         _service = new ShoppingCartServiceFake();
         _controller = new ShoppingCartController(_service);
     }
    

    Instead of a fake, the test class could have created other types of mocks, e.g. stubs or spies

    To test GetById, you would go through a similar process:

    • Refactor the Service implementation so it get its dependencies in the constructor;
    • Create mocks for the dependencies;
    • In the ServiceTest class, instantiate the mocks and inject them in the Service implementation;
    • Exercise the Service implementation and checking its behavior, assuming the mock behavior of the dependencies.

    OBS: The concerns of the article are not what most people call API Testing. It is really unit testing of class which happens to be the boundary of an API. API Testing usually refers to Integration Testing (or Integrated Testing to be more specific).



Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2