首页 > 解决方案 > 如何使用 Jasmine 使用 window.location.href.substring() 和 toPromise().then() 测试 Angular 9 服务的封面?

问题描述

我正在我的 Angular 程序上使用 Jasmine 进行一些单元测试,但我是它的初学者。即使在这里阅读了一些示例,我也无法涵盖我的代码的某些部分。

首先,这是我的 UserService :

export class UserService {
  user: User;
  role: Role;
  profile: string;
  headers: HttpHeaders;

  constructor(private http: HttpClient, private resources: Resources) {}

  getUserInit(): Promise<any> {
    let profile: string;
    let headers: HttpHeaders;
    const profilePosition = window.location.href.indexOf('to=');

    if (profilePosition > 0) {
      profile = window.location.href.substring(window.location.href.indexOf('to=') + 3);
    }

    if (profile) {
      headers = new HttpHeaders({
        profile,
        skip: 'true',
      });
    } else {
      headers = new HttpHeaders({
        skip: 'true',
      });
    }

    this.profile = profile;
    this.headers = headers;

    return this.http
      .get<User>(this.resources.connectedUser, { headers })
      .toPromise()
      .then((user) => {
        this.user = user;

        this.role = new Role(
          this.user.profileCode === Resources.MANAGE,
          this.user.profileCode === Resources.NAT,
          this.user.profileCode === Resources.BRA,
          this.user.profileCode === Resources.DPT
        );

        this.user.orgaList = [
          new Branch(1, null, new Department(null)),
          new Branch(2, null, new Department(null))
        ];

        return user;
      });
  }

  getUser() {
    return this.user;
  }

  getRole() {
    return this.role;
  }
}

现在,这是我的测试:

describe('UserService', () => {
  let service: UserService;
  let httpTestingController: HttpTestingController;
  let resources: Resources;

  const manageRole = RoleSample.getManageRole();
  const natRole = RoleSample.getNatRole();
  const braRole = RoleSample.getBranchRole();
  const dptRole = RoleSample.getDptRole();

  beforeEach(() => {
    TestBed.configureTestingModule({
      imports: [HttpClientTestingModule],
      providers: [UserService, Resources],
    });

    service = TestBed.inject(UserService);
    httpTestingController = TestBed.inject(HttpTestingController);
    resources = TestBed.inject(Resources);
  });
 
  it('should create the component', () => {
    expect(service).toBeTruthy();

  });

  it('should test the manager role', () => {
    service.role = new Role(true, false, false, false);
    expect(service.getRole()).toEqual(manageRole);
  });
    
  it("should test user initialization", () => {
    let userPromise = service.getUserInit();

    userPromise.then((user) => {
      expect(service.getUser()).toEqual(user);
    });
  });
});

有了这个,我无法涵盖这些部分(我想涵盖它们):

  1. profile = window.location.href.substring(window.location.href.indexOf('to=') + 3);
  2. 标头 = 新的 HttpHeaders
  3. (用户) => { this.user = 用户; this.role = 新角色
  4. this.user.orgaList = [
  5. 返回用户;
  6. 获取用户()

我想如果我可以涵盖 1 和 3,它可以涵盖所有内容。但我不知道该怎么做。我用 spyOn 尝试了很多解决方案,但都失败了。如果有人能解释我如何覆盖,至少,1和3,我真的很感谢他。

编辑:如评论中所问,这里是一些代码(需要类)。我有很多文件,希望我没有忘记任何文件。

export class Resources {
  public static MANAGE = 'test-manage';
  public static NAT = 'test-national';
  public static BRA = 'test-branch';
  public static DPT = 'test-department';
  public connectedUser = this.urlBackEnd + '/auth/whoami';
}

export class RoleSample {
  static getManageRole(): Role {
    return new Role(true, false, false, false);
  }

  static getNatRole(): Role {
    return new Role(false, true, false, false);
  }

  static getBranchRole(): Role {
    return new Role(false, false, true, false);
  }

  static getDptRole(): Role {
    return new Role(false, false, false, true);
  }
}

export class Role {
  constructor(
    public manage: boolean,
    public national: boolean,
    public branch: boolean,
    public department: boolean
  ) {}
} 

export class User {
  constructor(
    public username: string,
    public idAgent: number,
    public lastname: string,
    public firstname: string,
    public email: string,
    public profileCode: string,
    public orgaList: Branch[]
  ) {}
}

export class Branch {
  constructor(
    public id: number,
    public code: string,
    public department: Department,
  ) {}
}

export class Department {
  constructor(
    public id: number
  ) {}
}

标签: angulartypescriptkarma-jasminecode-coverage

解决方案


推荐阅读