大佬教程收集整理的这篇文章主要介绍了使用TypeMoq Mock和Angular TestBed,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
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,所以@R_720_10673@下
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与角度TESTBed一起使用,如果是这样,你如何正确地做到这一点?
更改
providers: [{ provide: Fooservice,useValue: mockFooservice.object}]
至
providers: [{ provide: Fooservice,useFactory: () => { return mockFooservice.object } }]
使用工厂函数返回为我摆脱了错误.如果您使用useClass,您将收到有关param.map的错误不是函数,如果您使用useValue,则会收到有关意外括号的错误.但是,useFactory只是一个返回moq.object的内联函数.
以上是大佬教程为你收集整理的使用TypeMoq Mock和Angular TestBed全部内容,希望文章能够帮你解决使用TypeMoq Mock和Angular TestBed所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。