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
- Delegate the dependencies and class-under-test (CUT) instantiation out of the test cases themselves
- Gives you full control over when and how the CUT is instantiated
- Setup individual test case preconditions in one place
- 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?
- Harder to control instantiation
- Relies on a module and component compilation (which takes time because it needs the styles and template files)
- 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 thebuilder
so we can reference it from the methods in the object itselffunction 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 thebuilder
to enable call chainingfunction setup() { const builder = { default() { return builder; } } return builder; }
- Add a
build
method attached to the builder object. It will return the Class under test instancefunction setup() { const builder = { default() { return builder; }, build() { return new ClassUnderTest(); } } return builder; }