Angular的NO_ERRORS_SCHEMA是否有问题? [英] Problems with Angular's NO_ERRORS_SCHEMA?

查看:85
本文介绍了Angular的NO_ERRORS_SCHEMA是否有问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在测试具有子组件的组件时,您可以选择以下几种方式:

When testing a component that has sub-components you have a few options:

  1. 导入用于设置组件和子组件的模块.问题是您实例化了真正的子组件及其依赖关系.
  2. 模拟子组件.取决于它具有多少个子组件,这可能很乏味.
  3. 使用NO_ERRORS_SCHEMA并忽略子组件.
  1. Import the module that sets up the component and sub-components. Problem is you instantiate the real sub-components and their dependencies.
  2. Mock sub-components. This can be tedious depending on how many sub-components it has.
  3. Use NO_ERRORS_SCHEMA and ignore sub-components.

鉴于大多数时候您只关心当前组件,为什么NO_ERRORS_SCHEMA在测试教程,博客文章等中没有得到更多关注?

Given that most of the time you only care about the current component, why isn't NO_ERRORS_SCHEMA given more attention in the testing tutorials, blog posts, etc.?

我知道它被标记为实验性的,但是从那以后我就不会对此感到困扰

I know it's flagged as experimental but I'm not bothered by that since

  1. 它与Angular的其余部分一起提供,并且
  2. 考虑到它的历史和即将到来的发布周期,整个Angular堆栈都处于试验阶段.

那么NO_ERRORS_SCHEMA是否有问题?还是只是偶然而已,没有得到更多提升?

So are there problems with NO_ERRORS_SCHEMA? Or is it just happenstance it's not promoted more?

推荐答案

我同意@jonsharpe的评估,在使用NO_ERROR_SCHEMA时,您隐藏了 any 模板错误,您可能会掩盖架构错误.

I agree with @jonsharpe's assessment in that, you hide any template errors when you use NO_ERROR_SCHEMA, you will likely mask schema errors.

使用TestBed导入模块的替代方法会导致依赖树地狱.

The alternative of importing the module with TestBed causes dependency-tree-hell.

还有第三种替代方法浅表渲染,它将自动模拟依赖项树,因此您通过单行测试设置即可获得类型安全性,模板安全性和依赖关系树解析.

There is a third alternative shallow-render that will mock your dependency tree automatically so you get type-safety, template safety and dependency-tree resolution with a single line of test setup.

全披露,我是浅表作者.

Full-disclosure, I am the author of shallow-render.

这篇关于Angular的NO_ERRORS_SCHEMA是否有问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆