【问题标题】:Write test case for angular 9 - google analytics events为 Angular 9 编写测试用例 - 谷歌分析事件
【发布时间】:2020-09-19 17:44:20
【问题描述】:

我如何在 jasmin karma 上为以下代码编写测试用例。 我什至不明白从哪里开始写下面的测试用例emitEvent 任何人都可以帮助我,这样我就可以清楚地了解事情的进展情况..

import { Injectable } from '@angular/core';

@Injectable()
export class GaEventsService {
  public emitEvent(
    eventLabel: string = null,
    eventCategory: string,
    eventAction: string,
    eventValue: number = null
  ) {
    (window as any).ga('send', 'event', {
      eventLabel,
      eventCategory,
      eventAction,
      eventValue,
    });
  }
}

这是我的测试用例 spec.ts

import { TestBed } from '@angular/core/testing';
import { GaEventsService } from './ga-events.service';

fdescribe('DateFormatService', () => {
  let service: GaEventsService;

  beforeEach(() => {
    TestBed.configureTestingModule({
      providers: [
        GaEventsService,
        // { provide: GaEventsService, useClass: GaEventsServiceMock },
      ],
    });
    service = TestBed.inject(GaEventsService);
  });

  it('should be created', () => {
    expect(service).toBeTruthy();
  });

  beforeAll(() => {
    // (<any>window).gtag=function() {} // if using gtag
    (window as any).ga = () => {};
  });

  it('should be created service', () => {
    expect(GaEventsService).toBeDefined();
  });

  it('should do something...', () => {
    const gaSpy = spyOn(service.emitEvent, 'ga');
    expect(gaSpy).toHaveBeenCalledWith('send', 'event', {
      eventLabel: 'eventLabel',
      eventCategory: 'eventCategory',
      eventAction: 'eventAction',
      eventValue: 'eventValue',
    });
  });
});

我猜这是未覆盖的线 - code coverage image

【问题讨论】:

  • 你不应该在你的代码中直接使用窗口对象。而是为它创建一个提供者并将其用作依赖项。这也可能使测试更容易编写。

标签: angular typescript unit-testing jasmine karma-jasmine


【解决方案1】:

阅读测试服务文档here

试试这个代码:

it('should do something...', () => {
    // insert here initialisation for variables eventLabel, eventCategory, eventAction, eventValue with appropriate values
    const gaSpy = spyOn((window as any), 'ga');
    service.emitEvent(eventLabel, eventCategory, eventAction, eventValue);
    expect(gaSpy).toHaveBeenCalledWith('send', 'event', {
      eventLabel,
      eventCategory,
      eventAction,
      eventValue,
    });
  });

【讨论】:

  • 我累了,但我做不到
  • 您是否收到错误或测试失败?什么不工作?
  • 测试的逻辑应该是这样的:你窥探到你想被调用的方法;您调用将触发此方法的方法;您断言该方法已被调用
  • @rohityadav 我已经更新了我的答案。如果它不起作用或您有疑问,请告诉我
【解决方案2】:
it('should do something...', () => {
    // insert here initialisation for variables eventLabel, eventCategory, eventAction, eventValue with appropriate values
    const gaSpy = spyOn(window as any, 'ga');
    service.emitEvent('eventLabel', 'eventCategory', 'eventAction', 1);
    expect(gaSpy).toHaveBeenCalledWith('send', 'event', {
      eventLabel: 'eventLabel',
      eventCategory: 'eventCategory',
      eventAction: 'eventAction',
      eventValue: 1,
    });
  });

是的,上面的代码是有效的,但我仍然很好奇为什么它没有被覆盖 100% 语句和分支@orlyohreally statements not covered - image

【讨论】:

  • @orlyohreally : 上面的代码工作但仍然分支是 50% 和语句是 77% 在完成所有代码库后
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多