简体   繁体   English

使用 Jasmine & Karma 测试 HTML 按钮

[英]Testing HTML Button with Jasmine & Karma

contact.component.html联系方式.component.html

<div>
    {{ text }}
</div>

<form id="contact-form" [formGroup] = "contactForm" novalidate>
    <div class = "form-group">
        <label class = "center-block">Name:
            <input class = "form-control" formControlName = "name">
        </label>
        <label class="center-block"> Email:
            <input class="form-control" formControlName="email">
        </label>
        <label class="center-block"> Text:
            <input class="form-control" formControlName="text">
        </label>
    </div>
    <button id="MyButton" click="onSubmit()">Click Me!</button>
</form>

contact.component.ts contact.component.ts

import { Component } from '@angular/core';
import { FormControl, FormGroup, Validators} from '@angular/forms';

@Component({
    templateUrl : './contact.component.html',
    styleUrls: ['./contact.component.sass']
})

export class ContactComponent {
    text = 'contact Page';
    contactForm: FormGroup;

    contact = {
        name: '',
        email: '',
        text: ''
    };
    submitted = false;

    constructor() {

    }

    onSubmit(): void {
        this.submitted = true;
    }
}

contact.component.spec.ts contact.component.spec.ts

import { BrowserModule, By } from '@angular/platform-browser';
import { TestBed, async, ComponentFixture } from '@angular/core/testing';
import { DebugElement } from '@angular/core';


import { FormsModule, ReactiveFormsModule } from '@angular/forms';
import { ContactComponent } from 'src/component/contact/contact.component';




describe('ContactComponent', () => {
    let comp: ContactComponent;
    let fixture: ComponentFixture<ContactComponent>;
    let de: DebugElement;
    let el: HTMLElement;
    beforeEach(async(() => {
        TestBed.configureTestingModule({
        declarations: [
            ContactComponent
        ],
        imports: [
            BrowserModule,
            FormsModule,
            ReactiveFormsModule
        ]
        }).compileComponents().then( () => {
            fixture = TestBed.createComponent(ContactComponent);
            comp = fixture.componentInstance;
        });
    }));

    it('should call the onSubmit method 1 times', async( () => {
        fixture.detectChanges();

        spyOn(comp, 'onSubmit');
        document.getElementById('MyButton').click();

        expect(comp.onSubmit).toHaveBeenCalledTimes(1);
    }));

These are my files.这些是我的文件。 I want to do a test on my onSubmit() function.我想对我的onSubmit()函数进行测试。 I Have a HTML Page with a button id='MyButton' .我有一个带有按钮id='MyButton'的 HTML 页面。 If I click the button the OnSubmit() function get called.如果我单击按钮OnSubmit()函数被调用。

If I run如果我跑

ng test测试

I get this:我明白了:

业力输出

It says: "Expected spy onSubmit to have been called once. It was called 0 times."它说:“预期 spy onSubmit 被调用一次。它被调用了 0 次。”

So how can I detect the onSubmit function to be called if I use a html Button?那么,如果我使用 html 按钮,如何检测要调用的onSubmit函数呢?

There are a few things that are not properly set up, but mainly the way you are setting up your click event.有一些设置不正确,但主要是您设置click事件的方式。 It should be (click)="onSubmit()" and not click="onSubmit()" .它应该是(click)="onSubmit()"而不是click="onSubmit()"

Also I would change the way you are setting up your spy to the following:此外,我会将您设置间谍的方式更改为以下内容:

let onSubmitSpy = spyOn(component, 'onSubmit').and.callThrough();
expect(onSubmitSpy).not.toHaveBeenCalled();

// more code ..
expect(onSubmitSpy).toHaveBeenCalledTimes(1);

Here is a working stackblitz. 是一个有效的stackblitz。

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

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