gpt4 book ai didi

angular - 为具有 NgZone 依赖项的组件运行 jasmine 测试

转载 作者:太空狗 更新时间:2023-10-29 17:30:04 26 4
gpt4 key购买 nike

我将如何继续为以下组件运行 jasmine 测试:

@Component({
moduleId: module.id,
selector: "testComp",
template: "<div>{{value}}</div>",
})
export class TestComp {
public value: string = "This is me";
constructor(public zone: NgZone) {
this.zone.run(() => console.log("zone is here"));
}
}

以下失败并显示无法解析 NgZone 的所有参数:

describe("test", () => {
let fixture;
let component;

beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [TestComp],
schemas: [NO_ERRORS_SCHEMA],
providers: [NgZone]
}).compileComponents;
}));
beforeEach(() => {
fixture = TestBed.createComponent(TestComp);
component = fixture.debugElement.componentInstance;
});
it("should check that the component is created", () => {
expect(component).toBeTruthy();
});

})

使用 Angular 4.1.3。我找到了 MockNgZone 类 @ https://no-shadow-angular-io.firebaseapp.com/docs/ts/latest/api/core/testing/MockNgZone-class.html .但是在这个特定版本的@angular/core/testing 中似乎不可用: enter image description here

有人知道我应该做什么来测试这个组件吗?

问候

最佳答案

如果你的问题是关于 runOutsideAngular 的,因为你不能使用 asyncfakeAsync,你唯一需要模拟的是那个函数和以下效果很好:

const ngZone = TestBed.get(NgZone);

spyOn(ngZone, 'runOutsideAngular').and.callFake((fn: Function) => fn());

关于angular - 为具有 NgZone 依赖项的组件运行 jasmine 测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47503064/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com