Component testing with dependencies

We have learned a lot already, but let's face it, no component that we build will be as simple as the one we wrote in the preceding section. There will almost certainly be at least one dependency, looking like this:

@Component({})
export class ExampleComponent {
constructor(dependency:Dependency) {}
}

We have different ways of dealing with testing such a situation. One thing is clear though: if we are testing the component, then we should not test the service as well. This means that when we set up such a test, the dependency should not be the real thing. There are different ways of dealing with that when it comes to unit testing; no solution is strictly better than the other:

  • Using a stub means that we tell the dependency injector to inject a stub that we provide, instead of the real thing
  • Injecting the real thing, but attaching a spy, to the method that we call in our component 

Regardless of the approach, we ensure that the test is not performing a side effect such as talking to a filesystem or attempting to communicate via HTTP; we are, using this approach, isolated.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset