使用 TestBed 对 angular 6 组件进行单元测试 (?)
Unit testing (?) an angular 6 component with TestBed
使用 TestBed
时,您是真正对组件进行单元测试还是进行集成测试?
创建夹具 (TestBed.createComponent(AppComponent)
) 并调用 fixture.detectChanges()
会自动调用 ngOnInit
。如果你想测试另一种方法,你现在正在测试多个单元。
这引出了另一个问题:您应该测试单元,还是应该测试用户操作?例如,您应该测试方法 setDimensions
还是应该测试当用户单击某个按钮时,元素具有适当的尺寸等等。
我想第一种测试方式会更接近于"unit test"方式,但是你仍然需要处理被调用组件的生命周期方法。这让我觉得没有办法使用 TestBed
对组件进行单元测试。存根所有生命周期方法似乎很荒谬。
无论您决定测试哪种方式,您也应该测试 DOM,不是吗?那么您就不会通过包含 DOM api.
来进行孤立测试
引用自Angular docs:
A component, unlike all other parts of an Angular application,
combines an HTML template and a TypeScript class. The component truly
is the template and the class working together. and to adequately test
a component, you should test that they work together as intended.
Such tests require creating the component's host element in the
browser DOM, as Angular does, and investigating the component class's
interaction with the DOM as described by its template.
The Angular TestBed facilitates this kind of testing as you'll see in
the sections below. But in many cases, testing the component class
alone, without DOM involvement, can validate much of the component's
behavior in an easier, more obvious way.
所以在这里,单元是一个组件(模板和class一起工作)。并且您应该尝试通过存根输入和依赖项来测试组件。
我想如果您从头到尾阅读一次测试文档,您就会在其中找到问题的答案。
这是从 Test Driven Development Wikipedia page.
收集的更多信息
For TDD, a unit is most commonly defined as a class, or a group of related functions often called a module. Keeping units relatively small is claimed to provide critical benefits [...]
所以单元测试不一定测试最小的单元。
Test-driven development does not perform sufficient testing in situations where full functional tests are required to determine success or failure, due to extensive use of unit tests.[21] Examples of these are user interfaces, programs that work with databases, and some that depend on specific network configurations. TDD encourages developers to put the minimum amount of code into such modules and to maximize the logic that is in testable library code, using fakes and mocks to represent the outside world.[22]
UI 可通过单元测试进行测试,直到某个递减 returns 点,此时功能性 tests/e2e 测试有用。
Unit tests are so named because they each test one unit of code. A complex module may have a thousand unit tests and a simple module may have only ten. The unit tests used for TDD should never cross process boundaries in a program, let alone network connections. Doing so introduces delays that make tests run slowly and discourage developers from running the whole suite. Introducing dependencies on external modules or data also turns unit tests into integration tests. If one module misbehaves in a chain of interrelated modules, it is not so immediately clear where to look for the cause of the failure.
我想我现在将集成测试定义为还测试应用程序外部部分的测试,例如其他进程,如数据库或服务器 API。
使用 TestBed
时,您是真正对组件进行单元测试还是进行集成测试?
创建夹具 (TestBed.createComponent(AppComponent)
) 并调用 fixture.detectChanges()
会自动调用 ngOnInit
。如果你想测试另一种方法,你现在正在测试多个单元。
这引出了另一个问题:您应该测试单元,还是应该测试用户操作?例如,您应该测试方法 setDimensions
还是应该测试当用户单击某个按钮时,元素具有适当的尺寸等等。
我想第一种测试方式会更接近于"unit test"方式,但是你仍然需要处理被调用组件的生命周期方法。这让我觉得没有办法使用 TestBed
对组件进行单元测试。存根所有生命周期方法似乎很荒谬。
无论您决定测试哪种方式,您也应该测试 DOM,不是吗?那么您就不会通过包含 DOM api.
来进行孤立测试引用自Angular docs:
A component, unlike all other parts of an Angular application, combines an HTML template and a TypeScript class. The component truly is the template and the class working together. and to adequately test a component, you should test that they work together as intended.
Such tests require creating the component's host element in the browser DOM, as Angular does, and investigating the component class's interaction with the DOM as described by its template.
The Angular TestBed facilitates this kind of testing as you'll see in the sections below. But in many cases, testing the component class alone, without DOM involvement, can validate much of the component's behavior in an easier, more obvious way.
所以在这里,单元是一个组件(模板和class一起工作)。并且您应该尝试通过存根输入和依赖项来测试组件。
我想如果您从头到尾阅读一次测试文档,您就会在其中找到问题的答案。
这是从 Test Driven Development Wikipedia page.
收集的更多信息For TDD, a unit is most commonly defined as a class, or a group of related functions often called a module. Keeping units relatively small is claimed to provide critical benefits [...]
所以单元测试不一定测试最小的单元。
Test-driven development does not perform sufficient testing in situations where full functional tests are required to determine success or failure, due to extensive use of unit tests.[21] Examples of these are user interfaces, programs that work with databases, and some that depend on specific network configurations. TDD encourages developers to put the minimum amount of code into such modules and to maximize the logic that is in testable library code, using fakes and mocks to represent the outside world.[22]
UI 可通过单元测试进行测试,直到某个递减 returns 点,此时功能性 tests/e2e 测试有用。
Unit tests are so named because they each test one unit of code. A complex module may have a thousand unit tests and a simple module may have only ten. The unit tests used for TDD should never cross process boundaries in a program, let alone network connections. Doing so introduces delays that make tests run slowly and discourage developers from running the whole suite. Introducing dependencies on external modules or data also turns unit tests into integration tests. If one module misbehaves in a chain of interrelated modules, it is not so immediately clear where to look for the cause of the failure.
我想我现在将集成测试定义为还测试应用程序外部部分的测试,例如其他进程,如数据库或服务器 API。