首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >为什么棱角材料工具提示破坏了我的单元测试?

为什么棱角材料工具提示破坏了我的单元测试?
EN

Stack Overflow用户
提问于 2017-07-06 18:39:50
回答 2查看 2.4K关注 0票数 2

我有以下单元测试文件,它是来自另一个类似组件的工作文件的副本,并且离样板角CLI输出不远:

代码语言:javascript
运行
复制
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 (Linux0.0.0) PageHeaderComponent应创建失败 错误:模板解析错误:无法绑定到“mdTooltipDisabled”,因为它不是“按钮”的已知属性。(“fxLayoutAlign=”启动中心“class=”文本介质“> ]mdTooltipDisabled="showHeaderTooltip"> view_agenda ]mdTooltipDisabled="showHeaderTooltip"> directions ]mdTooltipDisabled="showHeaderTooltip”href=“{environment?.search_url}”>/vendor.bundle.js (第25078行)解析@/vendor.bundle.js:25078:72 _compileTemplate@/vendor.bundle.js:39124:44 ……

下面是标记的一个示例:

代码语言:javascript
运行
复制
<button md-button mdTooltip="Topics" [mdTooltipDisabled]="showHeaderTooltip">
    <md-icon>view_agenda</md-icon> <span fxShow.xs="false">Topics</span>
</button>

这是我的版本表:

代码语言:javascript
运行
复制
@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

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-07-06 19:23:56

看一看这个:

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

“通过在失败的规范中将‘MaterialModule’导入TestBed.configureTestingModule进行修正。”

如:

代码语言:javascript
运行
复制
   TestBed.configureTestingModule({
        declarations: [PageHeaderComponent],
        providers: [
            {
                imports: [RouterTestingModule.withRoutes([]), MaterialModule],
                provide: Http,
                deps: [MockBackend]
            },
            PreloadService,
            UserService
        ],
        schemas: [CUSTOM_ELEMENTS_SCHEMA]
    });
票数 2
EN

Stack Overflow用户

发布于 2021-03-23 17:28:47

我今天遇到了这样的情况(如Sam在另一个响应中的评论),我需要将MatTooltipModule添加到我的导入中

代码语言:javascript
运行
复制
TestBed.configureTestingModule({
...
imports: [
  ...
  MatTooltipModule,
  ...
]
...
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44956622

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档