title: Angular2, testing and resolved data: How to test ngOnInit?
body: <p>I'm working through the <a href="https://angular.io/docs/ts/latest/guide/testing.html" rel="noreferrer">Angular2 testing guide</a> and wish to write a test for the <code>ngOnInit()</code> function. The one from the <a href="https://angular.io/guide/testing#routing-component" rel="noreferrer">Routing section of the programming guide</a> has this format:</p>
  <pre><code>let org: Org = null;
  
  ngOnInit(): void {
    let that = this;
  
    this.route.data
      .subscribe((data: { org: Org }) =&gt; {
        that.org = data.org;
      });
  }
  </code></pre>
  <p>This is fulfilled through a resolver, like:</p>
  <pre><code>resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable&lt;Org&gt; {
    let id = this.authService.user.orgId;
  
    return this.orgService
      .getOrg(id)
      .map(
        (org: Org) : Org =&gt; {
  
          if(org) {
            return org;
          } else { 
            // If  the Org isn't available then the edit page isn't appropriate.
            this.router.navigate(['/provider/home']);
            return null;
          }
       })
      .first();
  }
  </code></pre>
  <p>The code works OK, but I'm not sure how to write a test for <code>ngOnInit</code>.  The examples available to me assume that an embedded <code>OrgService</code> can be replaced by a <code>MockOrgService</code>.  However, all I have is a resolver.</p>
  <p>I'll eventually figure out how to test the resolver on its own.  Are there any useful tests I can do with a resolver-based <code>ngOnInit</code>?</p>