首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Angular2 -模拟Angular2-toaster for Jasmine单元测试

Angular2 -模拟Angular2-toaster for Jasmine单元测试
EN

Stack Overflow用户
提问于 2016-12-26 17:45:11
回答 0查看 787关注 0票数 2

更一般化的问题--如何模拟Angular2中使用的外部库

在我的应用程序中,我使用Angular2-toaster处理弹出消息。为了向其中一个组件编写单元测试套件(Jasmine),我尝试用以下代码模拟烤面包机的响应。

代码语言:javascript
复制
@Component({
selector: 'test-component',
template: '<toaster-container [toasterconfig]="toasterconfig"></toaster-container>',
})
export class TestComponent {
    toasterService: ToasterService;

constructor(toasterService: ToasterService) {
    this.toasterService = toasterService;
}

public toasterconfig: ToasterConfig = new ToasterConfig({ showCloseButton: true, tapToDismiss: false, timeout: 0, toastContainerId: 1 });
public toasterconfig2: ToasterConfig = new ToasterConfig({ showCloseButton: true, tapToDismiss: false, timeout: 0, toastContainerId: 2 });
}  

并添加到声明中

代码语言:javascript
复制
declarations: [RouterLinkStubDirective, TestComponent], 

但最终以错误告终

代码语言:javascript
复制
    Error: Error in ./ToasterContainerComponent class ToasterContainerComponent - inline template:1:34 caused by: undefined is not an object (evaluating 'klass.trim') in config/k
ma-test-shim.js (line 19495)
    _toggleClass@webpack:///~/@angular/common/bundles/common.umd.js:1280:0 <- config/karma-test-shim.js:43944:27
    webpack:///~/@angular/common/bundles/common.umd.js:1257:56 <- config/karma-test-shim.js:43921:84
    forEachAddedItem@webpack:///~/@angular/core/bundles/core.umd.js:3391:0 <- config/karma-test-shim.js:13459:20
    _applyIterableChanges@webpack:///~/@angular/common/bundles/common.umd.js:1257:0 <- config/karma-test-shim.js:43921:38
    ngDoCheck@webpack:///~/@angular/common/bundles/common.umd.js:1231:0 <- config/karma-test-shim.js:43895:48
    detectChangesInInputProps
    detectChangesInternal
    detectChanges@webpack:///~/@angular/core/bundles/core.umd.js:9305:0 <- config/karma-test-shim.js:19373:40
    detectChanges@webpack:///~/@angular/core/bundles/core.umd.js:9410:0 <- config/karma-test-shim.js:19478:53
    detectViewChildrenChanges@webpack:///~/@angular/core/bundles/core.umd.js:9331:0 <- config/karma-test-shim.js:19399:37
    detectChangesInternal
    detectChanges@webpack:///~/@angular/core/bundles/core.umd.js:9305:0 <- config/karma-test-shim.js:19373:40
    detectChanges@webpack:///~/@angular/core/bundles/core.umd.js:9410:0 <- config/karma-test-shim.js:19478:53
    detectViewChildrenChanges@webpack:///~/@angular/core/bundles/core.umd.js:9331:0 <- config/karma-test-shim.js:19399:37
    detectChangesInternal
    detectChanges@webpack:///~/@angular/core/bundles/core.umd.js:9305:0 <- config/karma-test-shim.js:19373:40
    detectChanges@webpack:///~/@angular/core/bundles/core.umd.js:9410:0 <- config/karma-test-shim.js:19478:53
    detectChanges@webpack:///~/@angular/core/bundles/core.umd.js:7398:0 <- config/karma-test-shim.js:17466:38
    _tick@webpack:///~/@angular/core/bundles/core-testing.umd.js:194:0 <- config/karma-test-shim.js:9213:50
    webpack:///~/@angular/core/bundles/core-testing.umd.js:208:45 <- config/karma-test-shim.js:9227:59
    invoke@webpack:///~/zone.js/dist/zone.js:232:0 <- config/karma-test-shim.js:6715:32
    onInvoke@webpack:///~/zone.js/dist/proxy.js:79:0 <- config/karma-test-shim.js:8344:46
    invoke@webpack:///~/zone.js/dist/zone.js:231:0 <- config/karma-test-shim.js:6714:41
    onInvoke@webpack:///~/@angular/core/bundles/core.umd.js:6206:0 <- config/karma-test-shim.js:16274:48
    invoke@webpack:///~/zone.js/dist/zone.js:231:0 <- config/karma-test-shim.js:6714:41
    run@webpack:///~/zone.js/dist/zone.js:114:0 <- config/karma-test-shim.js:6597:50
    run@webpack:///~/@angular/core/bundles/core.umd.js:6096:46 <- config/karma-test-shim.js:16164:70
    detectChanges@webpack:///~/@angular/core/bundles/core-testing.umd.js:208:0 <- config/karma-test-shim.js:9227:33
    webpack:///src/app/common/sample-search-widget/sample-search-widget-component.spec.ts:113:29 <- config/karma-test-shim.js:51370:31
    execute@webpack:///~/@angular/core/bundles/core-testing.umd.js:823:0 <- config/karma-test-shim.js:9842:29
    webpack:///~/@angular/core/bundles/core-testing.umd.js:910:32 <- config/karma-test-shim.js:9929:57
    invoke@webpack:///~/zone.js/dist/zone.js:232:0 <- config/karma-test-shim.js:6715:32
    onInvoke@webpack:///~/zone.js/dist/proxy.js:79:0 <- config/karma-test-shim.js:8344:46
    invoke@webpack:///~/zone.js/dist/zone.js:231:0 <- config/karma-test-shim.js:6714:41
    run@webpack:///~/zone.js/dist/zone.js:114:0 <- config/karma-test-shim.js:6597:50
    webpack:///~/zone.js/dist/jasmine-patch.js:102:0 <- config/karma-test-shim.js:8550:38
    webpack:///~/@angular/core/bundles/core-testing.umd.js:91:0 <- config/karma-test-shim.js:9110:36
    invoke@webpack:///~/zone.js/dist/zone.js:232:0 <- config/karma-test-shim.js:6715:32
    onInvoke@webpack:///~/zone.js/dist/async-test.js:49:0 <- config/karma-test-shim.js:8642:46
    onInvoke@webpack:///~/zone.js/dist/proxy.js:76:0 <- config/karma-test-shim.js:8341:48
    invoke@webpack:///~/zone.js/dist/zone.js:231:0 <- config/karma-test-shim.js:6714:41
    run@webpack:///~/zone.js/dist/zone.js:114:0 <- config/karma-test-shim.js:6597:50
    webpack:///~/@angular/core/bundles/core-testing.umd.js:86:0 <- config/karma-test-shim.js:9105:33
    webpack:///~/zone.js/dist/async-test.js:38:0 <- config/karma-test-shim.js:8631:47
    invokeTask@webpack:///~/zone.js/dist/zone.js:265:0 <- config/karma-test-shim.js:6748:41
    runTask@webpack:///~/zone.js/dist/zone.js:154:0 <- config/karma-test-shim.js:6637:58
    invoke@webpack:///~/zone.js/dist/zone.js:335:0 <- config/karma-test-shim.js:6818:41
    webpack:///~/zone.js/dist/zone.js:970:0 <- config/karma-test-shim.js:7453:31

    x Update trimmed value to model on blur of sample name
      Chrome 55.0.2883 (Windows 7 0.0.0)
    Error: Error in ./ToasterContainerComponent class ToasterContainerComponent - inline template:1:34 caused by: Cannot read property 'trim' of undefined
    TypeError: Cannot read property 'trim' of undefined
        at NgClass._toggleClass (webpack:///~/@angular/common/bundles/common.umd.js:1280:0 <- config/karma-test-shim.js:43944:27)
        at webpack:///~/@angular/common/bundles/common.umd.js:1257:56 <- config/karma-test-shim.js:43921:72
        at DefaultIterableDiffer.forEachAddedItem (webpack:///~/@angular/core/bundles/core.umd.js:3391:0 <- config/karma-test-shim.js:13459:18)
        at NgClass._applyIterableChanges (webpack:///~/@angular/common/bundles/common.umd.js:1257:0 <- config/karma-test-shim.js:43921:22)
        at NgClass.ngDoCheck (webpack:///~/@angular/common/bundles/common.umd.js:1231:0 <- config/karma-test-shim.js:43895:27)
        at Wrapper_NgClass.detectChangesInInputProps (/CommonModule/NgClass/wrapper.ngfactory.js:38:18)
        at _View_ToasterContainerComponent0.detectChangesInternal (/ToasterModule/ToasterContainerComponent/component.ngfactory.js:69:21)
        at _View_ToasterContainerComponent0.AppView.detectChanges (webpack:///~/@angular/core/bundles/core.umd.js:9305:0 <- config/karma-test-shim.js:19373:19)
        at _View_ToasterContainerComponent0.DebugAppView.detectChanges (webpack:///~/@angular/core/bundles/core.umd.js:9410:0 <- config/karma-test-shim.js:19478:49)
        at _View_SampleSearchWidgetComponent0.AppView.detectViewChildrenChanges (webpack:///~/@angular/core/bundles/core.umd.js:9331:0 <- config/karma-test-shim.js:19399:24)
        at _View_SampleSearchWidgetComponent0.detectChangesInternal (/DynamicTestModule/SampleSearchWidgetComponent/component.ngfactory.js:596:8)
        at _View_SampleSearchWidgetComponent0.AppView.detectChanges (webpack:///~/@angular/core/bundles/core.umd.js:9305:0 <- config/karma-test-shim.js:19373:19)
        at _View_SampleSearchWidgetComponent0.DebugAppView.detectChanges (webpack:///~/@angular/core/bundles/core.umd.js:9410:0 <- config/karma-test-shim.js:19478:49)
        at _View_SampleSearchWidgetComponent_Host0.AppView.detectViewChildrenChanges (webpack:///~/@angular/core/bundles/core.umd.js:9331:0 <- config/karma-test-shim.js:19399:24)
        at _View_SampleSearchWidgetComponent_Host0.detectChangesInternal (/DynamicTestModule/SampleSearchWidgetComponent/host.ngfactory.js:45:8)
        at _View_SampleSearchWidgetComponent_Host0.AppView.detectChanges (webpack:///~/@angular/core/bundles/core.umd.js:9305:0 <- config/karma-test-shim.js:19373:19)
        at _View_SampleSearchWidgetComponent_Host0.DebugAppView.detectChanges (webpack:///~/@angular/core/bundles/core.umd.js:9410:0 <- config/karma-test-shim.js:19478:49)

我不确定该走哪条路!

任何帮助我们都将不胜感激。

提前谢谢。

EN

回答

页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41329248

复制
相关文章

相似问题

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