Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/28.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 角度/因果报应中的模拟文档_Angular_Unit Testing_Karma Jasmine_Karma Runner - Fatal编程技术网

Angular 角度/因果报应中的模拟文档

Angular 角度/因果报应中的模拟文档,angular,unit-testing,karma-jasmine,karma-runner,Angular,Unit Testing,Karma Jasmine,Karma Runner,如何在Angular中模拟文档(HTMLDocument的阴影表示)?该实现在构造函数中使用以下内容: @Inject(DOCUMENT) private document: Document 查看后,我将其放入.spec设置中: const lazyPath = 'dummy'; const pathname = `/${lazyPath}`; const document = { location: { pathname } as Location } as Document; befor

如何在Angular中模拟文档(HTMLDocument的阴影表示)?该实现在构造函数中使用以下内容:

@Inject(DOCUMENT) private document: Document
查看后,我将其放入.spec设置中:

const lazyPath = 'dummy';
const pathname = `/${lazyPath}`;
const document = { location: { pathname } as Location } as Document;
beforeEachProviders(() => ([ {provide: DOCUMENT, useValue: document} ]));
但它给了我错误:

ERROR in ./src/app/main/components/app-lazy/app-lazy.component.spec.ts
Module not found: Error: Can't resolve '@angular/core/testing/src/testing_internal' in '...'
resolve '@angular/core/testing/src/testing_internal' in '....'
  Parsed request is a module
  using description file: .../package.json (relative path: ...)
    Field 'browser' doesn't contain a valid alias configuration
    resolve as module

当我在TestBed.configureTestingModule中使用一个简单的提供程序:[]而不是从testing\u内部包中使用BeforeAchProviders时,组件是未定义的,例如未正确初始化。当我从注入的文档切换到窗口对象(我无法在其上设置/模拟位置)时,它仅在单元测试中初始化(在非测试执行中,两者都起作用)。我能做什么?

将此作为答案发布,因为格式在注释中不起作用

如果可能的话,你能分享一次闪电战吗?当我需要注入模拟时,我通常将其设置为:

/。。。文件开头
const mockDocument={location:{pathname}};
在每个(()=>TestBed.configureTestingModule之前({
进口:[……],
//提供文件模拟
供应商:[
{提供:文档,使用值:mockDocument}
]
}));
//…文件的其余部分

将我的评论移到了一个答案,以帮助设置格式。您确定这样做有效吗?我的应用程序有点复杂,但将窗口更改为(注入的)文档是测试通过或失败的原因(带有TypeError:el.queryselectoral不是一个函数)。您的解决方案是我最初使用的解决方案,这是我得到的错误。您能发布一个最小stackblitz吗?“我已经有一段时间没用这个文件了,所以我可能有点生锈了。”亨伯托莫拉,你能再详细解释一下吗?如果您需要共享代码片段,可以打开一个新问题并链接到它。我有时间就去看看。帮我个大忙!好建议,谢谢!