title: How to test with Karma Jasmine the login.service function
body: <p>I tried to test e.g. in Jasmine within Angular2 the function login but I have not idea how can I do this? This would help me to understand it better. </p>
  
  <p>I tried:</p>
  
  <pre><code>import { TestBed, async } from '@angular/core/testing';
  import { LoginService } from './login.service';
  import { LoginComponent } from '../app/components/login/login.component';
  
  beforeEach(() =&gt; {
      TestBed.configureTestingModule({
          declarations: [LoginComponent],
          providers: [LoginService],
      });
  
      let fixture = TestBed.createComponent(LoginComponent);
      let comp = fixture.componentInstance;
  
      // TwainService actually injected into the component
      let loginService = fixture.debugElement.injector.get(LoginService);
  
      // Setup spy on the `getQuote` method
      let spy = spyOn(loginService, 'login')
          .and.returnValue('true');
  
  });
  </code></pre>
  
  <blockquote>
    <p><strong>login.component.ts</strong></p>
  </blockquote>
  
  <pre><code>login() {
  
            this.loading = true;
            this.loginService.login(this.model.username, this.model.password)
                .subscribe(result =&gt; {
                    console.log(result);
                    if (result == true) {
  
                        this.router.navigate(['/start']);
  
                    } else {
                      this.error = 'Wrong password';
                      this.errorStatus = true;
                      setTimeout(() =&gt; {
                        this.errorStatus = false;
                      }, 1500);
  
                        this.loading = false;
                    }
                });
        }
  </code></pre>