角单位测试错误:预期的间谍getDateFormat被称为

发布于 2025-02-10 14:51:41 字数 1613 浏览 1 评论 0 原文

我是对单位测试的新手,希望有人帮助我。 我有一个称为StudentComponent的角组件,并且具有以下内容。

    public getAllStudents(){    
        this.apollo.query({
            query: GETALL_STUDENTS,
        }).subscribe((result: any) => {
            this.students = result?.data?.getAllStudents;
            this.students.forEach(element => {
            element.Age = this.CalculateAge(element.DateOfBirth);
            element.DateOfBirthString = this.getDateFormat(element.DateOfBirth);
            });
        });
    }

    public CalculateAge(dateOfBirth: any): number {
        let age = 0;
        if (dateOfBirth) {
            var timeDiff = Math.abs(Date.now() - new Date(dateOfBirth).getTime());
            age = Math.floor(timeDiff / (1000 * 3600 * 24) / 365.25);
        }
        return age;
    }

    getDateFormat(date: Date): string{
        let dateString = ""
        if(date){
            dateString = formatDate(date, 'yyyy/MM/dd','en_US').toString();
        }
        return dateString;
    }

我写了测试案例,如下说

    describe('getAllStudents', () => {
        it('makes expected calls', () => {
          spyOn(component, 'getDateFormat').and.callThrough();
          spyOn(component, 'CalculateAge').and.callThrough();
          spyOn(apollo, 'query').and.callThrough();
          component.getAllStudents();
          expect(apollo.query).toHaveBeenCalled();
          expect(component.getDateFormat).toHaveBeenCalled();
          expect(component.CalculateAge).toHaveBeenCalled();
  
        });
      });

给了我一个错误。 “预计间谍getDateFormat被称为。”有人可以指导我解决这个问题吗?

I'm new to unit testing and hope someone help me.
I have a angular component called StudentComponent and it has following.

    public getAllStudents(){    
        this.apollo.query({
            query: GETALL_STUDENTS,
        }).subscribe((result: any) => {
            this.students = result?.data?.getAllStudents;
            this.students.forEach(element => {
            element.Age = this.CalculateAge(element.DateOfBirth);
            element.DateOfBirthString = this.getDateFormat(element.DateOfBirth);
            });
        });
    }

    public CalculateAge(dateOfBirth: any): number {
        let age = 0;
        if (dateOfBirth) {
            var timeDiff = Math.abs(Date.now() - new Date(dateOfBirth).getTime());
            age = Math.floor(timeDiff / (1000 * 3600 * 24) / 365.25);
        }
        return age;
    }

    getDateFormat(date: Date): string{
        let dateString = ""
        if(date){
            dateString = formatDate(date, 'yyyy/MM/dd','en_US').toString();
        }
        return dateString;
    }

I wrote the test case like following

    describe('getAllStudents', () => {
        it('makes expected calls', () => {
          spyOn(component, 'getDateFormat').and.callThrough();
          spyOn(component, 'CalculateAge').and.callThrough();
          spyOn(apollo, 'query').and.callThrough();
          component.getAllStudents();
          expect(apollo.query).toHaveBeenCalled();
          expect(component.getDateFormat).toHaveBeenCalled();
          expect(component.CalculateAge).toHaveBeenCalled();
  
        });
      });

this gave me an error. 'Expected spy getDateFormat to have been called.' Could anyone please guide me to resolve this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

╰沐子 2025-02-17 14:51:41

问题是 getallstudents 包括异步代码。使用 observable.subscribe ,您的代码将收到有关可用结果的延迟通知,很可能在期望已评估后。

存在使用 Jasmine 测试异步代码的不同方法(请参阅摘自茉莉花文档)。

Angular 还提供了测试异步代码的功能。例如,您可以在 tick() 调用 getAllstudents()。这看起来如下:

it('makes expected calls', fakeAsync(() => {

  // given
  spyOn(component, 'getDateFormat').and.callThrough();
  spyOn(component, 'CalculateAge').and.callThrough();
  spyOn(apollo, 'query').and.callThrough();

  // when
  component.getAllStudents();
  tick();

  // then
  expect(apollo.query).toHaveBeenCalled();
  expect(component.getDateFormat).toHaveBeenCalled();
  expect(component.CalculateAge).toHaveBeenCalled();
}));

请注意,您正在测试实现方法,但不是该方法的结果。将方法 getAllStudents()作为黑匣子,然后检查所有 dateofbirthstring /代码>设置为预期。

The problem is that getAllStudents includes asynchronous code. With Observable.subscribe, your code will receive a delayed notification of the available result, most probably after expect has been evaluated.

There exist different ways to test asynchronous code with Jasmine (see Asynchronous Work from Jasmine documentation).

Angular also provides functionality to test asynchronous code. You can for example execute your unit test in the fakeAsync zone. Then, you need to invoke tick() just after calling getAllStudents(). This could look as follows:

it('makes expected calls', fakeAsync(() => {

  // given
  spyOn(component, 'getDateFormat').and.callThrough();
  spyOn(component, 'CalculateAge').and.callThrough();
  spyOn(apollo, 'query').and.callThrough();

  // when
  component.getAllStudents();
  tick();

  // then
  expect(apollo.query).toHaveBeenCalled();
  expect(component.getDateFormat).toHaveBeenCalled();
  expect(component.CalculateAge).toHaveBeenCalled();
}));

Please note that you're testing the implementation but not the result of the method. It would make more sense to consider the method getAllStudents() as a black box and check that Age and DateOfBirthString on all students are set as expected.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文