Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/2.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 CLI应用程序中使用Karma执行单元测试时未找到任何对象_Angular_Karma Runner - Fatal编程技术网

在Angular CLI应用程序中使用Karma执行单元测试时未找到任何对象

在Angular CLI应用程序中使用Karma执行单元测试时未找到任何对象,angular,karma-runner,Angular,Karma Runner,我已经用Angular CLI设置了我的项目。我还通过“ng g component NAME”创建了我的组件。这也自动添加了一个测试类来创建我的单元测试(这当然非常简洁) 问题是我的测试很难失败,即使我让它们保持初始状态。我想在写任何测试之前先测试一下。然而,测试失败得可怕。没有一行执行得很好 > ng test 'app-some-selector' is not a known element: 1. If 'app-some-selector' is an Angular com

我已经用Angular CLI设置了我的项目。我还通过“ng g component NAME”创建了我的组件。这也自动添加了一个测试类来创建我的单元测试(这当然非常简洁)

问题是我的测试很难失败,即使我让它们保持初始状态。我想在写任何测试之前先测试一下。然而,测试失败得可怕。没有一行执行得很好

> ng test

'app-some-selector' is not a known element:
1. If 'app-some-selector' is an Angular component, then verify that it is part of this module.
比如说

The pipe 'myPipe' could not be found 
对于特定组件中的每个对象都会引发上述错误。如果通过选择器使用另一个组件,则无法找到相应的组件;如果通过依赖项注入使用http之类的服务,则编译器会声明没有该组件的提供程序,依此类推


我的项目在
ng serve
btw中运行良好。

当您通过angular cli创建组件时,它会为您创建

it('should create', () => {
    expect(component).toBeTruthy();
});
第一个测试尝试创建一个组件,如果您添加了一个dependancie,例如其他组件,它就会下降

要通过此测试,您需要在每个函数之前更新在测试类中创建组件的方式:

beforeEach(async(() => {
    TestBed.configureTestingModule({
      declarations: [ myComponent ],
      providers: [myService]
    })
    .compileComponents();
}));

这里,如果我在组件测试依赖性注入中添加服务
myService
。这样,您可以将模拟对象作为
myService
更容易地测试组件。

当您通过angular cli创建组件时,它会为您创建

it('should create', () => {
    expect(component).toBeTruthy();
});
第一个测试尝试创建一个组件,如果您添加了一个dependancie,例如其他组件,它就会下降

要通过此测试,您需要在每个函数之前更新在测试类中创建组件的方式:

beforeEach(async(() => {
    TestBed.configureTestingModule({
      declarations: [ myComponent ],
      providers: [myService]
    })
    .compileComponents();
}));
这里,如果我在组件测试依赖性注入中添加服务
myService
。这样,您可以将模拟对象作为
myService
更容易地测试组件