angular - 为什么 Angular Material 工具提示会破坏我的单元测试?

标签 angular unit-testing angular-material angular-material2

我有以下单元测试文件,它是另一个类似组件的工作文件的副本,与样板 Angular CLI 输出相差不远:

import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { Http, HttpModule } from '@angular/http';
import { MockBackend } from '@angular/http/testing';
import { CUSTOM_ELEMENTS_SCHEMA } from '@angular/core';

import { PageHeaderComponent } from './page-header.component';
import { UserService } from '../user.service';
import { PreloadService } from '../preload.service';

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

    beforeEach(() => {
        TestBed.configureTestingModule({
            declarations: [PageHeaderComponent],
            providers: [
                {
                    provide: Http,
                    deps: [MockBackend]
                },
                PreloadService,
                UserService
            ],
            schemas: [CUSTOM_ELEMENTS_SCHEMA]
        });

        fixture = TestBed.createComponent(PageHeaderComponent);
        component = fixture.componentInstance;
    });

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

但是,由于 mdTooltip 的问题,这个失败了。它会导致此错误:

PhantomJS 2.1.1 (Linux 0.0.0) PageHeaderComponent should be created FAILED

Error: Template parse errors: Can't bind to 'mdTooltipDisabled' since it isn't a known property of 'button'. ("fxLayoutAlign="start center" class="text-medium"> ][mdTooltipDisabled]="showHeaderTooltip"> view_agenda ][mdTooltipDisabled]="showHeaderTooltip"> directions ][mdTooltipDisabled]="showHeaderTooltip" href="{{environment?.search_url}}"> http://localhost:9876/_karma_webpack_/vendor.bundle.js (line 25078) parse@http://localhost:9876/_karma_webpack_/vendor.bundle.js:25078:72 _compileTemplate@http://localhost:9876/_karma_webpack_/vendor.bundle.js:39124:44
....

以下是标记示例:

<button md-button mdTooltip="Topics" [mdTooltipDisabled]="showHeaderTooltip">
    <md-icon>view_agenda</md-icon> <span fxShow.xs="false">Topics</span>
</button>

这是我的版本表:

@angular/cli: 1.1.2
node: 6.11.0
os: linux x64
@angular/animations: 4.2.3
@angular/common: 4.2.3
@angular/compiler: 4.2.3
@angular/core: 4.2.3
@angular/forms: 4.2.3
@angular/http: 4.2.3
@angular/platform-browser: 4.2.3
@angular/platform-browser-dynamic: 4.2.3
@angular/router: 4.2.3
@angular/cli: 1.1.2
@angular/compiler-cli: 4.2.3
@angular/flex-layout: 2.0.0-beta.8
@angular/language-service: 4.2.3
@angular/material: 2.0.0-beta.7

我需要提供什么来模拟或以其他方式处理 mdTooltip 服务,这显然涉及可观察的?

更新:显然,其他 AM 服务也会发生这种情况,例如 mdMenuTriggerFor

最佳答案

看看这个:

https://github.com/angular/material2/issues/2478

“通过将‘MaterialModule’导入失败规范中的 TestBed.configureTestingModule 来修复。”

如:

   TestBed.configureTestingModule({
        declarations: [PageHeaderComponent],
        providers: [
            {
                imports: [RouterTestingModule.withRoutes([]), MaterialModule],
                provide: Http,
                deps: [MockBackend]
            },
            PreloadService,
            UserService
        ],
        schemas: [CUSTOM_ELEMENTS_SCHEMA]
    });

关于angular - 为什么 Angular Material 工具提示会破坏我的单元测试?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44956622/

相关文章:

Angular 多选下拉菜单

c# - 我的模拟 DbContext 使用 Moq 在单元测试中写入数据库

angular-material - 设置角度分量默认值

javascript - 如何在 <md-radio-group> 中使用输入字段作为自定义条目?

Angular 组件,检查属性是否存在

angular - rxjs/运营商 : can not find module

angular - 如何完成 rxjs switchmap observable?

unit-testing - 使用 OpenEJB 进行 EJB 存储库测试 - 如何回滚更改

javascript - 我可以像单元测试一样在 Node.js 中进行性能测试吗

javascript - Angular Material datepicker 手动打开