Skip to content

Latest commit

 

History

History
95 lines (88 loc) · 4.65 KB

04 - Stubbing dependencies.md

File metadata and controls

95 lines (88 loc) · 4.65 KB

Practical 4 - Stubbing Dependencies

Objectives

  • Identify dependencies to be controlled when testing production code
  • Install a stub library & get access to it in your test code
  • Learn how to stub a method on a dependency and control its return value
  • Write a passing test using a stubbed dependency

Extensions

  • Refactor repetatives tasks out into test set-up (beforeEach) and tear-down (afterEach) hooks
  • Read up on how Angular handles dependency management and how you might control dependencies in this environment. This is much more similar to how static languages such as C# and Java manage dependencies.

Set-up steps

Same pattern as previous pracs

  1. Commit any outstanding changes on your Prac03 branch: git commit -am 'Some message'
  2. Head back over to master: git checkout master
  3. Check out the Start Point for Prac 4: git checkout Prac04-StartPoint
  4. Create a branch to make your changes on without impacting the main repo: git checkout -b Prac04

Practical Steps

  1. Install Sinon and add it as a dev dependency for our project
  2. We need to write some tests for pet-service.js. Identify the dependency to be stubbed.
  3. Create a new test file for pet-service.js
  4. Get a reference to the code we're testing
  5. Get references to Sinon and HttpClient
  6. Add a describe block for PetService
  7. Nest a describe block for getting pet information
  8. Add an it block for testing the transformation of the JSON result
  9. Find out what the JSON result from the external API is going to look like
  10. Create a mock return result for test purposes
  11. Instruct sinon to stub the get method on httpClient, and resolve its promise with our fake result
  12. Call the service and assert on the result
  13. Instruct sinon to release the stub and restore the original functionality
  14. Run the test and see a fast, green result
  15. Ensure that the actual httpClient.get method isn't being called

Solutions

Here's a step-by-step walkthrough of the practical steps, for if you get stuck :)

  1. Install Sinon and add it as a dev dependency for our project
    • npm install sinon --save-dev
  2. We need to write some tests for pet-service.js. Identify the dependency to be stubbed.
    • At the top of pet-service.js, it requires http-client.js
    • pet-service uses http-client makes a call off to an external API
    • To test that quickly and reliably we need to stub the http-client
  3. Create a new test file for pet-service.js
    • Create a new file test\pet-service-tests.js
  4. Get a reference to the code we're testing
    • var petService = require('../src/pet-service');
  5. Get references to Sinon and HttpClient
    • var sinon = require('sinon');
    • var httpClient = require('../src/http-client');
  6. Add a describe block for PetService
    • describe('PetService', function() { ... });
  7. Nest a describe block for getting pet information
    • describe('when retrieving pet details', function() { ... });
  8. Add an it block for testing the transformation of the JSON result
    • it('returns a nicely consumable JSON result', function() { ... });
  9. Find out what the JSON result from the external API is going to look like
    • Get https://juniordev-refactor.azurewebsites.net/api/Pets?petName=Beaglier
    • Inspect the structure of the JSON result
  10. Create a mock return result for test purposes
    var externalApiJsonResult = {
        "AnimalVariety": "Dog",
        "AnimalInformation": "Beagliers are beautiful",
        "ReferenceSource": "https://en.wikipedia.org/wiki/Beaglier",
        "Base64EncodedVisualDepiction": "base64dog"
    };
  11. Instruct sinon to stub the get method on httpClient, and resolve its promise with our fake result
    • sinon.stub(httpClient, 'get');
    • httpClient.get.resolves(externalApiJsonResult);
  12. Call the service and assert on the result
    return petService.getPetDetails("Beaglier")
        .then(result => {
            result.should.have.property("info").which.equals("Beagliers are beautiful");
            result.should.have.property("source").which.equals("https://en.wikipedia.org/wiki/Beaglier");
            result.should.have.property("image").which.equals("base64dog");
        });
  13. Instruct sinon to release the stub and restore the original functionality
    • httpClient.get.restore();
  14. Run the test and see a fast, green result
  15. Ensure that the actual httpClient.get method isn't being called
    • You could set a breakpoint in the actual get method and debug, or add a console.log entry to the actual get method to ensure nothing is logged in the console during the test run