Recipies - gparlakov/scuri GitHub Wiki

I'll try to give a guide of how I envisioned the setup function to be used.

Setup function goals

  1. Delegate the dependencies and class-under-test (CUT) instantiation out of the test cases themselves
  2. Gives you full control over when and how the CUT is instantiated
  3. Setup individual test case preconditions in one place
  4. Allows for you to test the constructor (even though it's not recommended to have logic in there - it happens to be needed)

Why not use TestBed?

  1. Harder to control instantiation
  2. Relies on a module and component compilation (which takes time because it needs the styles and template files)
  3. Does not offer a mechanism of externalizing single test case prerequisites

RECIPE Initialize your setup function

Using Scuri:

ng g scuri:spec --name my/my.component.ts

Manual

  • In your spec file add a top-level function (i.e. accessible from any spot in the file) called setup.
    function setup() {
    
    }
    
  • Have it return a builder object. Declaring the builder so we can reference it from the methods in the object itself
    function setup() {
       const builder = {
    
       }
    
       return builder;
    }
    
  • Add a default method attached to the builder object. It will be used to setup the default, working and non-exception-throwing instance of the class under test. Method must return the builder to enable call chaining
    function setup() {
       const builder = {
           default() {
               return builder;
           }
       }
    
       return builder;
    }
    
  • Add a build method attached to the builder object. It will return the Class under test instance
    function setup() {
       const builder = {
           default() {
               return builder;
           },
           build() {
               return new ClassUnderTest();
           }
       }
    
       return builder;
    }