title: Angular 2 unit test
body: <p>Got a couple of problems with my unit testing with Jasmine. First one:</p>
  
  <p>I need to test this in a Component called CaseList:</p>
  
  <pre class="lang-ts prettyprint-override"><code>gotoDetail(case: Case){
    this._router.navigate(['CaseDetail', {"id": case.id}]);
  }
  </code></pre>
  
  <p>All my attempts at the tests give the error is this._router is undefined, well that's because I haven't defined it in my test, as I can't figure out how! I haven't even come up with any good attempts at the tests, as I have no idea how to proceed. So that's why I haven't posted any attempt here...</p>
  
  <p>Edit: The part in the router-test which is related to to the problem above, but I test all the routing in a separate file! This test works!</p>
  
  <pre class="lang-ts prettyprint-override"><code>it('Should navigate to Case Detail List', (done) =&gt; {   
  router.navigate(['CaseDetail', {id: 'test'}]).then(() =&gt; {
          expect(location.path()).toEqual('/casedetail/test');
          done();
      }).catch(e =&gt; done.fail(e));
  });
  </code></pre>
  
  <p>Second tests from a Detail Component (where user is navigated after choosing case) :</p>
  
  <pre class="lang-ts prettyprint-override"><code>addStep(){
    this.case.getSteps().push(new Step());
  }
  </code></pre>
  
  <p>I also have a remove method I need to test:</p>
  
  <pre class="lang-ts prettyprint-override"><code>removeStep(step: Step){
    this.case.removeStep(step);
  }
  </code></pre>
  
  <p>Constructor for this component:</p>
  
  <pre class="lang-ts prettyprint-override"><code>constructor(public _routeParams: RouteParams, public _service: Service) {
    this.case = _service.getById(_routeParams.get('id'));
  }
  </code></pre>
  
  <p>So the test I tried doing for the add-method:</p>
  
  <pre class="lang-ts prettyprint-override"><code>it('passes new step to case-class', () =&gt; {
    spyOn(case, 'addStep')
    .and.returnValue(Observable.of({complete: true}))
    caseDetail.addStep();
    expect(case.addStep).toHaveBeenCalledWith(step);
  });
  </code></pre>
  
  <p>So these methods call the methods that are in a separate class called "Case".</p>
  
  <p>The error I'm getting when testing these are that case is null. I guess the routing and service messes it up, as in the same Component I have a other "identical" methods, and testing those works fine. But they belong to a different class.</p>
  
  <p>Method in same component, referring to a "Step"-class:</p>
  
  <pre class="lang-ts prettyprint-override"><code>addFeedback(step: Step){
    step.addFeedback(new Feedback());
  }
  </code></pre>
  
  <p>The testing works perfectly:</p>
  
  <pre class="lang-ts prettyprint-override"><code>it('passes feedback value to Step class', () =&gt; {
    spyOn(step, 'addFeedback')
    .and.returnValue(Observable.of({complete: true}))
    caseDetail.addFeedback(step);
    expect(step.addFeedback).toHaveBeenCalledWith(feedback);
  })
  </code></pre>
  
  <p>So obviously in testing the component I should have everything defined that is needed, since the testing of the feedback method works. I just need to define the "case" object somehow, so that it doesn't complain about it being null.</p>
  
  <p>Well hopefully you get my problem at hand and hopefully you can help! :)</p>