angular - 将 TypeMoq Mock 与 Angular TestBed 结合使用

标签 angular unit-testing typescript jasmine

我定义了一个 FooService 如下

import {Injectable} from "@angular/core";
export interface Foo {
    Foo(): string;
}

@Injectable()
export class FooService implements Foo {
    Foo(): string {
        return "Fooey!";
    }
}

和一个像这样的BarComponent

import {Component} from "@angular/core";
import {FooService} from "./foo.service";

@Component({
    moduleId: 'module.id',
    template: '<h1>Bar Component</h1>'
})
export class BarComponent {
    constructor(private fooService: FooService) {}

    doFoo(): string {
        return(this.fooService.Foo());
    }
}

现在我想测试我的 BarComponent 并且我想使用 TypeMoq 来模拟 FooService,所以我做了以下操作

import * as TypeMoq from 'typemoq';
import {Foo, FooService} from "./foo.service";
import {TestBed, async} from "@angular/core/testing";
import {BarComponent} from "./bar.component";

describe('BarComponent', () => {
    let component: BarComponent;
    let mockFooService: TypeMoq.IMock<Foo>;

    beforeEach(async(() => {
        mockFooService = TypeMoq.Mock.ofType<Foo>();
        TestBed.configureTestingModule({
            declarations: [BarComponent],
            providers: [{ provide: FooService, useValue: mockFooService.object}]
        });
    }));

    beforeEach(() => {
        let fixture = TestBed.createComponent(BarComponent);
        component = fixture.componentInstance;
    });

    it('does something', () => {
        mockFooService.setup(x => x.Foo()).returns(() => "FooBar!");
        expect(component.doFoo()).toEqual("FooBar!");
    });

});

但是运行上面的命令会出现以下错误

SyntaxError: Function arg string contains parenthesis
        at new Function (<anonymous>)
        at evalExpression (webpack:///~/@angular/compiler/@angular/compiler.es5.js:25431:25 <- config/karma-test-shim.js:59412:40)
        at jitStatements (webpack:///~/@angular/compiler/@angular/compiler.es5.js:25448:0 <- config/karma-test-shim.js:59429:12)
        at JitCompiler._compileModule (webpack:///~/@angular/compiler/@angular/compiler.es5.js:25658:0 <- config/karma-test-shim.js:59639:35)
        at createResult (webpack:///~/@angular/compiler/@angular/compiler.es5.js:25613:0 <- config/karma-test-shim.js:59594:106)
        at JitCompiler._compileModuleAndAllComponents (webpack:///~/@angular/compiler/@angular/compiler.es5.js:25616:0 <- config/karma-test-shim.js:59597:40)
        at JitCompiler.compileModuleAndAllComponentsSync (webpack:///~/@angular/compiler/@angular/compiler.es5.js:25559:0 <- config/karma-test-shim.js:59540:23)
        at TestingCompilerImpl.compileModuleAndAllComponentsSync (webpack:///~/@angular/compiler/@angular/compiler/testing.es5.js:475:0 <- config/karma-test-shim.js:68201:31)
        at TestBed._initIfNeeded (webpack:///~/@angular/core/@angular/core/testing.es5.js:705:0 <- config/karma-test-shim.js:21376:36)
        at TestBed.createComponent (webpack:///~/@angular/core/@angular/core/testing.es5.js:791:0 <- config/karma-test-shim.js:21462:14)
        at Function.TestBed.createComponent (webpack:///~/@angular/core/@angular/core/testing.es5.js:610:0 <- config/karma-test-shim.js:21281:29)
        at Object.<anonymous> (webpack:///src/app/auth/login/bar.component.spec.ts:19:30 <- config/karma-test-shim.js:99954:41)
        at ZoneDelegate.invoke (webpack:///~/zone.js/dist/zone.js:365:0 <- config/karma-test-shim.js:65763:26)
        at ProxyZoneSpec.onInvoke (webpack:///~/zone.js/dist/proxy.js:79:0 <- config/karma-test-shim.js:65294:39)
        at ZoneDelegate.invoke (webpack:///~/zone.js/dist/zone.js:364:0 <- config/karma-test-shim.js:65762:32)
        at Zone.run (webpack:///~/zone.js/dist/zone.js:125:0 <- config/karma-test-shim.js:65523:43)
        at Object.<anonymous> (webpack:///~/zone.js/dist/jasmine-patch.js:104:0 <- config/karma-test-shim.js:65010:34)
        at webpack:///~/@angular/core/@angular/core/testing.es5.js:96:0 <- config/karma-test-shim.js:20767:17
        at ZoneDelegate.invoke (webpack:///~/zone.js/dist/zone.js:365:0 <- config/karma-test-shim.js:65763:26)
        at AsyncTestZoneSpec.onInvoke (webpack:///~/zone.js/dist/async-test.js:49:0 <- config/karma-test-shim.js:64605:39)
        at ProxyZoneSpec.onInvoke (webpack:///~/zone.js/dist/proxy.js:76:0 <- config/karma-test-shim.js:65291:39)
        at ZoneDelegate.invoke (webpack:///~/zone.js/dist/zone.js:364:0 <- config/karma-test-shim.js:65762:32)
        at Zone.run (webpack:///~/zone.js/dist/zone.js:125:0 <- config/karma-test-shim.js:65523:43)
        at AsyncTestZoneSpec._finishCallback (webpack:///~/@angular/core/@angular/core/testing.es5.js:91:0 <- config/karma-test-shim.js:20762:25)
        at webpack:///~/zone.js/dist/async-test.js:38:0 <- config/karma-test-shim.js:64594:31
        at ZoneDelegate.invokeTask (webpack:///~/zone.js/dist/zone.js:398:0 <- config/karma-test-shim.js:65796:31)
        at Zone.runTask (webpack:///~/zone.js/dist/zone.js:165:0 <- config/karma-test-shim.js:65563:47)
        at ZoneTask.invoke (webpack:///~/zone.js/dist/zone.js:460:0 <- config/karma-test-shim.js:65858:38)
        at timer (webpack:///~/zone.js/dist/zone.js:1732:0 <- config/karma-test-shim.js:67130:29)

是否可以将 TypeMoq 与 Angular TestBed 一起使用?如果可以,您如何正确使用它?

最佳答案

我也遇到了这个问题,它的出现是因为你如何处理你的供应商。

改变

providers: [{ provide: FooService, useValue: mockFooService.object}]

providers: [{ provide: FooService, useFactory: () => { return mockFooService.object } }]

使用工厂函数返回为我消除了错误。如果您使用 useClass,您将收到关于 param.map is not a function 的错误,如果您使用 useValue,您将收到关于意外括号的错误。不过,useFactory 和一个返回 moq.object 的内联函数是有效的。

关于angular - 将 TypeMoq Mock 与 Angular TestBed 结合使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44272282/

相关文章:

javascript - 如何声明函数的函数属性?

typescript - 忽略 TS6133 : "(import) is declared but never used"?

javascript - Angular 2 和 karma-jasmine 单元测试用例在单击链接时打开模式

html - 如何在 Angular 中自动滚动?

unit-testing - 使用 angular 2 配置 karma webpack 的覆盖范围

c++ - Boost.Test output_test_stream 因模板化输出运算符而失败

unit-testing - 使用 throwsA 进行 Dart 单元测试

javascript - 在 Nginx 代理后面刷新页面后,我的 Angular 6 路由返回 404

angular - 从焦点上的 Material 输入和离开焦点时移除标签

c# - 要验证模拟,需要返回以验证