Javascript 组件道具即使在成功的角度测试后也是空的
在过去的两天里,我一直在努力找出一个不断失败的角度测试的问题。在Javascript 组件道具即使在成功的角度测试后也是空的,javascript,angular,jasmine,Javascript,Angular,Jasmine,在过去的两天里,我一直在努力找出一个不断失败的角度测试的问题。在Promise回调上设置组件道具,但在同一Promise成功回调上进行监视时,道具不会更新,也不会在浏览器中对日志进行控制台操作。e、 g以下测试通过和道具更新无任何问题: fit('should call insuranceSerivce.search', () => { component.travelPeriod = ['01-08-2019', '01-08-09']; component.destin
Promise
回调上设置组件道具,但在同一Promise
成功回调上进行监视时,道具不会更新,也不会在浏览器中对日志进行控制台操作。e、 g以下测试通过和道具更新无任何问题:
fit('should call insuranceSerivce.search', () => {
component.travelPeriod = ['01-08-2019', '01-08-09'];
component.destination = 'MYS';
component.tripType.individual = 'days';
component.onContinue();
expect(insuranceServiceSpy.search).toHaveBeenCalled();
});
但是,如果我取消注释plan
prop预期,则以下测试将失败
fit('should send the success response to "onSearchInsuranceSuccess"', fakeAsync(() => {
spyOn(component, 'onSearchInsuranceSuccess');
component.travelPeriod = ['01-08-2019', '01-08-09'];
component.destination = 'MYS';
component.tripType.individual = 'days';
component.onContinue();
fixture.detectChanges();
tick();
expect(component.onSearchInsuranceSuccess).toHaveBeenCalled();
console.log('[[[plans at spec]]]', component.plans); // Why is it always empty?
// expect(component.plans[0].plan).toEqual(insurance.data[0].plan); <-- it fails here
// expect(component.benefitsList[0].title).toEqual(insurance.benefits[0].title);
}));
以下是spec
[[plan before]] []length: 0__proto__: Array(0)
context.js:255 [[plan after]] (3) [{…}, {…}, {…}]0: {plan: "Plan 1", fare: {…}, details: {…}, supplierID: 17}1: {plan: "Plan 2", fare: {…}, details: {…}, supplierID: 17}2: {plan: "Plan 3", fare: {…}, details: {…}, supplierID: 17}length: 3__proto__: Array(0)
context.js:255 [[plan before]] []length: 0__proto__: Array(0)
context.js:255 [[[plans at spec]]] []length: 0__proto__: Array(0)
以及上述控制台
输出的一部分
谢谢问题可能是间谍电话。默认情况下,您使用它创建一个不做任何事情的存根
因此,在您的案例中,监视onSearchInsuranceSuccess
会停止执行内部的任何内容,但允许您测试是否执行了该方法
有一些方法可以让你定义间谍应该做什么。通常是像间谍(…)和.returnValue之类的东西。如果您希望spy只执行它通常执行的操作,则必须使用spyOn(…).and.callThrough()
设置spy
显示间谍的所有可用方法。问题可能是spyOn
调用。默认情况下,您创建一个不做任何事情的存根。因此,在您的案例中,监视onSearchInsuranceSuccess
会停止执行内部的任何内容,但允许您测试是否执行了该方法。如果您想监视并让其余部分执行,则必须使用spyOn(…).和.callThrough()设置监视。如果要调用某个测试方法,可以使用callFake()。但在你的情况下,你确实希望这个方法能像我想的那样,正常工作。所以callThrough将是一个采取。@Erbsenkoenig这真是太棒了。请你把你的评论作为回答,这样我就可以接受了?另外,是什么导致了错误:队列中仍有2个计时器。
此错误?请您也添加一些关于错误:队列中仍有2个计时器问题的注释。
此错误通常与并非所有异步代码都已完成有关。但要想真正帮上忙,我需要一次stackblitz来重现你的错误。否则,它只是猜测队列中还有哪些可观察对象。因为没有可观察对象,只有一个在spec
中定义的Promise
。在你的身体里也有一个。我需要知道它在组件内部的实际使用位置等。因此,如果不了解组件和测试设置如何一起工作,就很难告诉您问题出在哪里。
[[plan before]] []length: 0__proto__: Array(0)
context.js:255 [[plan after]] (3) [{…}, {…}, {…}]0: {plan: "Plan 1", fare: {…}, details: {…}, supplierID: 17}1: {plan: "Plan 2", fare: {…}, details: {…}, supplierID: 17}2: {plan: "Plan 3", fare: {…}, details: {…}, supplierID: 17}length: 3__proto__: Array(0)
context.js:255 [[plan before]] []length: 0__proto__: Array(0)
context.js:255 [[[plans at spec]]] []length: 0__proto__: Array(0)