简体   繁体   English

我应该如何测试这个角度分量?

[英]How should I test this angular component?

Say if I have a component which emits an event when clicked, ie 假设我有一个单击时会发出事件的组件,即

    @Component({
      selector: 'component-checkout-payment',
      template: `<button (click)="click()>Click me</button>`
    })
    export class TestComponent{

      @Output() clicked = new EventEmitter<boolean>();

      click() {
        this.clicked.emit(true);
      }
    }

Would I then test the event and the component method separately like below? 然后,我将像下面分别测试事件 component方法吗?

it('should emit an event when `click` is called`, () => {
  jest.spyOn(component.clicked, 'emit');
  component.click();
  expect(component.clicked.emit).toHaveBeenCalledWith(true);
});

it('should call `click` when button has been clicked`, () => {
  jest.spyOn(component, 'click');
  let button = fixture.debugElement.nativeElement.querySelector('button');
  button.click();
  expect(component.click).toHaveBeenCalled();
})

Or would I test that when the button has been clicked, that in turn it emits an event? 还是我会测试一下,当单击按钮时,它又发出一个事件吗?

it('should emit event when button has been clicked`, () => {
  jest.spyOn(component.clicked, 'emit');
  let button = fixture.debugElement.nativeElement.querySelector('button');
  button.click();
  expect(component.clicked.emit).toHaveBeenCalledWith(true)
})

I'm assuming the latter as it covers both the top two tests into one. 我假设使用后者,因为它将前两项测试合而为一。 However I would like clarification. 但是,我想澄清一下。

I would also like to know whether these kind of tests are unit tests, integration tests or e2e tests. 我还想知道这类测试是单元测试,集成测试还是e2e测试。

The two approaches are valid, it's really up to your preference. 两种方法都是有效的,这完全取决于您的偏好。

And those are unit tests. 这些是单元测试。 They prevent side effects in your application : if the test fails, it means your function has been modified. 它们防止了应用程序中的副作用:如果测试失败,则表明您的功能已被修改。

It's not an intgration testing because you test a single component, and it's not end-to-end testing because you don't go all the way through the back-end. 这不是一个集成测试,因为您只测试一个组件,也不是端到端测试,因为您没有一路经过后端。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM