繁体   English   中英

用茉莉花测试材料设计角组件

[英]Testing Material Design Angular components with jasmine

Edit2 :更具体地讲,使用诸如Material Button之类的东西效果很好。 但是,Side Nav在以下设置中不能很好地工作。

编辑 :我进行了更多的测试,发现这与Angular Material特别相关,因为没有它就不会发生这些错误。 我仍然不确定如何解决。

我正在尝试为新组件设置一些基本测试,但我不断遇到错误。 具体来说,在将@ angular / material添加到项目之后。 当前错误是:

Error: Found the synthetic listener @transform.start. Please include either "BrowserAnimationsModule" or "NoopAnimationsModule" in your application.

这对我来说就像是一条红鲱鱼。

规格如下:

import { async, ComponentFixture, TestBed } from '@angular/core/testing';

import { RouterTestingModule } from '@angular/router/testing';
import { MatSidenavModule } from '@angular/material/sidenav';

import { AngularNavigationComponent } from './angular-navigation.component';

describe('AngularNavigationComponent', () => {
    let component: AngularNavigationComponent;
    let fixture: ComponentFixture<AngularNavigationComponent>;

    beforeEach(
        async(() => {
            TestBed.configureTestingModule({
                declarations: [AngularNavigationComponent],
                imports: [RouterTestingModule, MatSidenavModule]
            }).compileComponents();
        })
    );

    beforeEach(() => {
        fixture = TestBed.createComponent(AngularNavigationComponent);
        component = fixture.componentInstance;
        fixture.detectChanges();
    });

    it('should create', () => {
        expect(component).toBeTruthy();
    });
});

这是组件

import { Component, OnInit } from '@angular/core';
import { MatSidenavModule } from '@angular/material/sidenav';
import { RouterLink } from '@angular/router';

@Component({
    selector: 'mysupercustomcat-navigation',
    templateUrl: './angular-navigation.component.html',
    styleUrls: ['./angular-navigation.component.scss']
})
export class AngularNavigationComponent implements OnInit {
    constructor() {}

    ngOnInit() {}
}

谁能告诉我我做错了什么,也许还可以在Testbed配置区域中解释声明与导入?

编辑:如果这很重要,这是一个混合应用程序

我添加了两个关键部分来避免这样的错误:

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

添加到页面顶部,并且:

schemas: [NO_ERRORS_SCHEMA],

在与声明相同的级别添加。

这是做什么的? 这告诉angular在未知元素或属性上不要出错。 现在,这适用于这种使用情况下 ,因为我不是在集成测试超级感兴趣。 我相信在这种情况下可能会引起问题。

我对单元测试感兴趣,忽略模板属性对我来说很好。 添加这个让我达到那个水平。

这个答案是目前我所能得到的最完整的答案。

暂无
暂无

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

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