title: Angular Unit test for an injected service
body: <p>I'm new to writing unit tests and trying to figure out writing a test for an injected service in a component.</p>
  
  <p><strong>Component:</strong></p>
  
  <pre><code>constructor(private _assessmentService: AssessmentDataService){
  
  }
  
  ngOnInit(){
      const assessmentSub: Subscription = this._assessmentService.getAssessmentQuestions().subscribe(responseData =&gt; {
          const obj = responseData.assessment;
          assessmentSub.unsubscribe();
      });
  }
  </code></pre>
  
  <p><strong>Service:</strong></p>
  
  <pre><code>getAssessmentQuestions(){
      return this._http.get('./assets/data/data.json').map(response =&gt; response.json());
  }
  </code></pre>
  
  <p><strong>Mock Service:</strong></p>
  
  <pre><code>export const assessmentMockData = {
  'assessment': [{
      'id': 1,
      'question': 'Qeustion 1',
      'options': [
          {
              'id': 1,
              'option': 'option 1',
              'score': 20
          },
          {
              'id': 2,
              'option': 'option 1',
              'score': 30
          },
          {
              'id': 3,
              'option': 'option 1',
              'score': 10
          },
          {
              'id': 4,
              'option': 'option 1',
              'score': 0
          }
      ],
      'selected': 2
  }]
  }
  
  export class AssessmentMockDataService{
      getAssessment(){
          return Observable.of({});
      }
  }
  </code></pre>
  
  <p><strong>Spec on the above component:</strong></p>
  
  <pre><code>beforeEach(async(() =&gt; {
  TestBed.configureTestingModule({
    imports: [ReactiveFormsModule, FormsModule],
    declarations: [AssessmentComponent, QuestionsComponent],
    providers: [
      { provide: AssessmentDataService, useClass: AssessmentMockDataService }
    ]
  })
  .compileComponents();
  
    }));
  
    beforeEach(() =&gt; {
      fixture = TestBed.createComponent(AssessmentComponent);
      let assessmentService: any = fixture.debugElement.injector.get(AssessmentDataService);
      spyOn(assessmentService, 'getAssessmentQuestions').and.returnValue(Observable.of(assessmentMockData));
      fixture.detectChanges();
    });
  </code></pre>
  
  <p>When I run <code>ng test -sm=false</code>, I get the error</p>
  
  <p><strong>Error:  : getAssessmentQuestions() method does not exist</strong></p>
  
  <p>What am I doing wrong here? </p>