Karma测试用例在导入使用全局可用接口的服务时给出错误

时间:2018-07-26 14:03:54

标签: angular typescript karma-jasmine angular-testing

控制台错误:src / app / services / auth / login.service.ts(27,18):错误TS2304:找不到名称“ LoginModel”。

我将提到以下文件->

  1. app / components / login / login.component.ts
  2. app / components / login / login.component.spec.ts
  3. app / components / login / login.model.ts

  1. app / services / login.service.ts

login.model.ts 包含

interface LoginModel {
    success: boolean;
}

login.service.ts 包含(使用其中的LoginModel)

      import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Router } from '@angular/router';
import { Observable, of } from 'rxjs';
import { UtilityService } from '../../shared/util/utility.service';
import { map } from '../../../../node_modules/rxjs/operators';

@Injectable({
  providedIn: 'root'
})
export class LoginService {

  private username;
  private password;
  // store the URL so we can redirect after logging in
  public redirectUrl: any;

  constructor(private http: HttpClient,
    private router: Router,
    private utils: UtilityService) { }

  attemptLogin(username, password) {
    this.username = username.trim();
    this.password = password;
    return this.http.post(<post_body>, {}, {}).pipe(
      map((data: LoginModel) => {

      }));
  }
}

所以这里使用的是上面定义的接口(这里没有进行导入或导出)

login.component.ts

    import { Component, OnInit, OnDestroy } from '@angular/core';
import { FormGroup, FormControl, Validators } from '@angular/forms';
import { LoginService } from '../../../services/auth/login.service';
import { Router, ActivatedRoute } from '@angular/router';
import { Subscription } from 'rxjs';

@Component({
  selector: 'app-login',
  templateUrl: './login.component.html',
  styleUrls: ['./login.component.scss']
})
export class LoginComponent implements OnInit, OnDestroy {

  loginForm: FormGroup;
  constructor(
    private loginService: LoginService,
    ) { }

  ngOnInit() {
    // reset login status
    this.loginService.logOut().toPromise().then(data => console.log('Logged Out Successfully'));

    this.loginForm = new FormGroup({
      email: new FormControl('tetsing', [Validators.required]),
      password: new FormControl('testing', [Validators.required]),
    });
  }
}

规格文件包含

import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { ReactiveFormsModule, FormsModule } from '@angular/forms';
import { RouterTestingModule } from '@angular/router/testing';
import { LoginComponent } from './login.component';
import { LoginService } from './../../../services/auth/login.service';

describe('LoginComponent', () => {
let component: LoginComponent;
let fixture: ComponentFixture<LoginComponent>;

beforeEach(async(() => {
  TestBed.configureTestingModule({
    imports: [ReactiveFormsModule, FormsModule, RouterTestingModule],
    declarations: [ LoginComponent ],

  })
  .compileComponents();
}));

beforeEach(() => {
  fixture = TestBed.createComponent(LoginComponent);
  component = fixture.componentInstance;
  fixture.detectChanges();
});

it('should create instance', () => {
  expect(component).toBeTruthy();
});
});

现在,当我运行此测试用例时,它给出了不知道LoginModel(登录服务中使用的类型)是什么的错误。如何使它可用于测试用例?

0 个答案:

没有答案