业力-ChangeDetectorRef detectChanges模拟 [英] Karma - ChangeDetectorRef detectChanges mocking
问题描述
我的测试中cdr.detectChanges()有问题.调用它时发生错误,我没有任何信息,我只收到此错误:
I have an issue with cdr.detectChanges() in my test. I have an error happening when it's called and I don't have any information, I just get this error :
ZoneAwareError @ webpack:///~/zone.js/dist/zone.js:923:0<- config/karma-shim.js:108645:28 invokeTask @ webpack:///~/zone.js/dist/zone.js:398:0<-config/karma-shim.js:108120:36 onInvokeTask @ webpack:///~/zone.js/dist/proxy.js:103:0<-config/karma-shim.js:107642:49
ZoneAwareError@webpack:///~/zone.js/dist/zone.js:923:0 <- config/karma-shim.js:108645:28 invokeTask@webpack:///~/zone.js/dist/zone.js:398:0 <- config/karma-shim.js:108120:36 onInvokeTask@webpack:///~/zone.js/dist/proxy.js:103:0 <- config/karma-shim.js:107642:49
有没有办法使detectChanges起作用?
Is there a way to make detectChanges work?
我还尝试通过在我的测试组件配置中定义cdr来使detectChanges什么都不做来尝试其他事情:
I also tried something else by trying to make detectChanges do nothing by defining cdr in my test component configuration like this :
cdr = {
detectChanges: () => {}
}
,然后在提供程序{ provide: ChangeDetectorRef, useValue: cdr }
中,但是当我在console.info中测试了方法中的cdr时,似乎我仍然拥有原始的cdr类.
and then in the providers { provide: ChangeDetectorRef, useValue: cdr }
, but when I console.info the cdr in the method tested it seems I still have the original cdr class.
我还尝试了一个伪类定义如下:
I also tried with a fake class defined like this :
class FakeCDR {
detectChanges(): void {
}
}
,然后在提供程序中单击{ provide: ChangeDetectorRef, useClass: FakeCDR }
,但在此处相同.
and then { provide: ChangeDetectorRef, useClass: FakeCDR }
in the providers, but same here.
这是我的测试代码:
it('should create an user', fakeAsync(inject([MockBackend],
(backend: MockBackend) => {
fixture.detectChanges()
tick()
component.newUser = User.newUserDraft()
component.newUser.email = "email@gmail.com"
component.createUser()
tick()
expect(component.newUser.email).toBe(User.newUserDraft().email)
})))
并在createUser中执行一项服务.createUser然后执行一些操作,然后执行cdr.detectChanges().我确实把console.info放在每一行,所以我确定它是在detectChanges上失败了.
and in createUser it's doing a service.createUser then do some stuff then cdr.detectChanges(). I did put console.info at every lines so I'm sure it's at the detectChanges that it fails.
推荐答案
首先要考虑的事项:
-
ChangeDetectorRef
不是通过DI提供的,因此您不能提供双精度数. -
fixture.changeDetectorRef
与提供的组件不同,因此您不能使用它. -
fixture.debugElement.injector.get(ChangeDetectorRef)
将创建私有类ViewRef
的新实例(公共类ViewRef
只是ViewRef$1
的别名),每次调用时,提供给组件的对象是另一个
ChangeDetectorRef
is not provided through DI, so you cannot provide a double.fixture.changeDetectorRef
is not the same as the component provided, so you cannot use this.fixture.debugElement.injector.get(ChangeDetectorRef)
will create a new instance of the private classViewRef
(the public classViewRef
is just an alias forViewRef$1
), every time it is invoked, then the object provided to the component is another.
所以我的解决方法是:
const changeDetectorRef = fixture.debugElement.injector.get(ChangeDetectorRef);
const detectChangesSpy = spyOn(changeDetectorRef.constructor.prototype, 'detectChanges');
这篇关于业力-ChangeDetectorRef detectChanges模拟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!