Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/3.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
Javascript 如何使用外部模块设置角度测试_Javascript_Angular_Unit Testing - Fatal编程技术网

Javascript 如何使用外部模块设置角度测试

Javascript 如何使用外部模块设置角度测试,javascript,angular,unit-testing,Javascript,Angular,Unit Testing,我想将模块从外部包括到我的angular 6(带cli)测试环境中,但出现以下错误: TypeError: Cannot read property 'injector' of null at TestBed.push.../node_modules/@angular/core/fesm5/testing.js.TestBed._createCompilerAndModule (http://localhost:9876/_karma_webpack_/webpack:/node_modules

我想将模块从外部包括到我的angular 6(带cli)测试环境中,但出现以下错误:

TypeError: Cannot read property 'injector' of null
at TestBed.push.../node_modules/@angular/core/fesm5/testing.js.TestBed._createCompilerAndModule (http://localhost:9876/_karma_webpack_/webpack:/node_modules/@angular/core/fesm5/testing.js:1030:1)
at TestBed.push.../node_modules/@angular/core/fesm5/testing.js.TestBed.compileComponents (http://localhost:9876/_karma_webpack_/webpack:/node_modules/@angular/core/fesm5/testing.js:945:1)
at Function.push.../node_modules/@angular/core/fesm5/testing.js.TestBed.compileComponents (http://localhost:9876/_karma_webpack_/webpack:/node_modules/@angular/core/fesm5/testing.js:805:46)
at UserContext.<anonymous> (http://localhost:9876/_karma_webpack_/webpack:/src/tested/components/tested.component.spec.ts:11:8)
at ZoneDelegate.push../node_modules/zone.js/dist/zone.js.ZoneDelegate.invoke (http://localhost:9876/_karma_webpack_/webpack:/node_modules/zone.js/dist/zone.js:388:1)
at AsyncTestZoneSpec.push../node_modules/zone.js/dist/zone-testing.js.AsyncTestZoneSpec.onInvoke (http://localhost:9876/_karma_webpack_/webpack:/node_modules/zone.js/dist/zone-testing.js:713:1)
at ProxyZoneSpec.push../node_modules/zone.js/dist/zone-testing.js.ProxyZoneSpec.onInvoke (http://localhost:9876/_karma_webpack_/webpack:/node_modules/zone.js/dist/zone-testing.js:285:1)
at ZoneDelegate.push../node_modules/zone.js/dist/zone.js.ZoneDelegate.invoke (http://localhost:9876/_karma_webpack_/webpack:/node_modules/zone.js/dist/zone.js:387:1)
at Zone.push../node_modules/zone.js/dist/zone.js.Zone.runGuarded (http://localhost:9876/_karma_webpack_/webpack:/node_modules/zone.js/dist/zone.js:151:1)
at runInTestZone (http://localhost:9876/_karma_webpack_/webpack:/node_modules/zone.js/dist/zone-testing.js:841:1)
tsconfig.spec.json

"include": [
    "../../src/**/*.spec.ts", // new
    "**/*.spec.ts",
    "**/*.d.ts"
  ]
它拉入外部模块和组件并运行测试,但在外部模块和组件上失败

我也尝试过类似的事情,但运气不佳:

测试.ts

// Then we find all the tests.
const context = require.context('./', true, /\.spec\.ts$/);
const contextExternal = require.context('./../../src/', true, /\.spec\.ts$/); // new

// And load the modules.
context.keys().map(context);
contextExternal.keys().map(contextExternal); // new
// First, initialize the Angular testing environment.
getTestBed().resetTestEnvironment(); // new
getTestBed().initTestEnvironment(
  BrowserDynamicTestingModule,
  platformBrowserDynamicTesting()
);
环境

├── @angular-devkit/build-angular@0.6.8
├── @angular/animations@6.0.9
├── @angular/cli@6.0.8
├── @angular/common@6.0.9
├── @angular/compiler@6.0.9
├── @angular/compiler-cli@6.0.9
├── @angular/core@6.0.9
├── @angular/forms@6.0.9
├── @angular/http@6.0.9
├── @angular/language-service@6.0.9
├── @angular/platform-browser@6.0.9
├── @angular/platform-browser-dynamic@6.0.9

解决方案是在外部模块的每次测试中重置测试环境

describe('TestedComponent', () => {

  beforeEach(async(() => {

    TestBed.resetTestEnvironment(); // new
    TestBed.initTestEnvironment(BrowserDynamicTestingModule, platformBrowserDynamicTesting()); // new

    TestBed.configureTestingModule({
      declarations: [
        TestedComponent
      ],
    }).compileComponents();

  }));

  it('should create TestedComponent', async(() => {
    const fixture = TestBed.createComponent(TestedComponent);
    const app = fixture.debugElement.componentInstance;
    expect(app).toBeTruthy();
  }));

});
describe('TestedComponent', () => {

  beforeEach(async(() => {

    TestBed.resetTestEnvironment(); // new
    TestBed.initTestEnvironment(BrowserDynamicTestingModule, platformBrowserDynamicTesting()); // new

    TestBed.configureTestingModule({
      declarations: [
        TestedComponent
      ],
    }).compileComponents();

  }));

  it('should create TestedComponent', async(() => {
    const fixture = TestBed.createComponent(TestedComponent);
    const app = fixture.debugElement.componentInstance;
    expect(app).toBeTruthy();
  }));

});