Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/30.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 由于动画中的query()调用,角度单元测试失败_Angular_Unit Testing_Jasmine - Fatal编程技术网

Angular 由于动画中的query()调用,角度单元测试失败

Angular 由于动画中的query()调用,角度单元测试失败,angular,unit-testing,jasmine,Angular,Unit Testing,Jasmine,我有一个条目组件,它包含登录表单和注册表单(每一个都是它们自己的组件)。entry组件包含一个变量,说明是显示login组件还是register组件,这是使用模板中的元素进行切换的 当切换此变量时,包装子组件的容器将在其从login=>register更改时设置动画(反之亦然) 当我在单元测试中单击此元素切换到注册表表单时,测试失败,因为我需要在单击切换后调用fixture.detectChanges(),以便与注册表表单实例交互。调用fixture.detectChanges()会导致以下错误

我有一个条目组件,它包含登录表单和注册表单(每一个都是它们自己的组件)。entry组件包含一个变量,说明是显示login组件还是register组件,这是使用模板中的元素进行切换的

当切换此变量时,包装子组件的容器将在其从login=>register更改时设置动画(反之亦然)

当我在单元测试中单击此元素切换到注册表表单时,测试失败,因为我需要在单击切换后调用
fixture.detectChanges()
,以便与注册表表单实例交互。调用fixture.detectChanges()会导致以下错误

Error: Unable to process animations due to the following failed trigger transitions @entryModeTransition has failed due to: 

- `query("app-login-form > form")` returned zero elements. (Use `query("app-login-form > form", { optional: true })` if you wish to allow this.)
beforeach()块中还有一个
fixture.detectChanges()
调用

我已经确保在测试设置中包含
NoopAnimationsModule
,但是这似乎并不能阻止动画触发(我认为
NoopAnimationsModule
可以)

我可以简单地将
{optional:true}
选项添加到动画定义中的
query()
调用中,但是我不喜欢在整个动画中添加此选项,因为它们只是为了防止测试失败

如果相关,则使用
ng mock
模拟登录和注册表单组件


有什么方法可以阻止动画在单元测试中运行吗?

如果其他人遇到这个问题,我最终会在规范设置中构建组件时覆盖动画来解决这个问题

这样,我仍然可以像在现有测试中一样模拟子组件,并且也不需要调整动画定义

通过将
animations
meta选项设置为一个数组,该数组包含一个空触发器定义,该定义与导致错误的动画同名

beforeEach(async(() => {
    TestBed.configureTestingModule({
      declarations: [EntryComponent, MockComponents(LoginFormComponent, RegisterFormComponent)],
      imports: [NoopAnimationsModule, ...other imports omitted],
      providers: [...providers omitted]
    })
      .overrideComponent(EntryComponent, {
        set: {
          animations: [trigger('entryModeTransition', [])]
        }
      })
      .compileComponents();
  }));
如果许多测试都需要此变通方法,则空触发器的创建甚至可以移动到实用程序函数,这可以整理覆盖:

.overrideComponent(EntryComponent, {
  set: {
    animations: mockAnimations(['entryFormTransition, 'someOtherTransition'])
  }
})

好吧,不要模拟组件,或者用不会使查询无效的假组件模拟它们:您的代码需要表单存在,因此如果它们没有表单,它就会中断。