Skip to content

Latest commit

 

History

History
59 lines (35 loc) · 2.52 KB

unit_testing.md

File metadata and controls

59 lines (35 loc) · 2.52 KB

1. Unit Testing 5%

The Anatomy of a Unit Test:

  1. Arrange - test setup
  2. Act - calling the tested method
  3. Assert - comparing with right result

Modules Used for Node.js Unit Testing:


Spies, stubs and mocks - which one and when?

  1. Spies - you can use spies to get information on function calls, like how many times they were called, or what arguments were passed to them.
  2. Stubs - stubs are like spies, but they replace the target function. You can use stubs to control a method's behaviour to force a code path (like throwing errors) or to prevent calls to external resources (like HTTP APIs).
  3. Mocks - mock is a fake method with a pre-programmed behavior and expectations.

Stubs VS Mocks

Stubs are dummy and always return hard-coded value whereas Mocks are usually configurable and comes with some internal implementation (for testing purposes).


Testing pyramid

testing pyramid

  • E2E - testing of the whole system (simulating the end user)
  • Integration - testing of the group of interconnected modules
  • Unit - testing each module in isolated environment (using stubs and mocks for the outside world)

TDD VS BDD

TDD

  • In TDD, I don’t care much about the output. The only thing needed is to carry out the test in a particular way.
  • TDD may lack the ability to specify the exact behavior, but you achieve higher quality with software code.

TDD workflow

tdd workflow

BDD

  • In BDD, I don’t mind how you come up with the output, only that the output has to be correct under the GIVEN condition.
  • In BDD you will come across a better specification since communication between the software developer and product owner is fast and easy.

BDD workflow

bdd workflow