如何在 Angular 中将模拟库安装到单元测试中?我想将 sinon-chrome 安装到旁观者测试中

How to install a mock library into a unit test in Angular? I want to install sinon-chrome into a spectator test

我正在开发 Google Chrome 扩展,它是用 angular 编写的,我需要为使用 Chrome API。我找到了一个我认为可以胜任的图书馆。

https://www.npmjs.com/package/sinon-chrome

但是,我没有足够的 Angular 经验,不知道如何准确地使用这个库。 usage section says that I need to require the value in a constant, but I have no idea how or where that would go. Does that go in the unit test? Does that go in the karma file? What is the global.chrome that is found in the examples.

我的单元测试代码。除此 chrome 模拟问题外,它在其他方面运行良好。

const angularFireAuthMock = {
    // eslint-disable-next-line @typescript-eslint/no-empty-function
    onAuthStateChanged: () => {},
};
 
describe('AppComponent', () => {
    let spectator: Spectator<AppComponent>;
    const createComponent = createComponentFactory({ component: AppComponent, mocks: [FirestoreService], providers: [{
        provide: AngularFireAuth, useValue: angularFireAuthMock,
    }] });
 
    beforeEach(() => {
        spectator = createComponent();
    });
 
    it('should create the app', () => {
        expect(spectator.component).toBeDefined();
    });
});

AppComponent 是 Chrome

的位置

感谢您的帮助。

我建议您只检查现有示例。

转到他们的github:https://github.com/acvetkov/sinon-chrome

在右边你可以看到“Used by”。去那里:https://github.com/acvetkov/sinon-chrome/network/dependents?package_id=UGFja2FnZS0xMzc4MTk5MA%3D%3D

然后通过 repo 转到 repo 并在搜索栏中搜索“sinon-chrome”(不带引号)

这应该向您展示其他人如何使用该软件包,我希望它能解答您的问题。