[英]Angular Unit Testing: Is this code worth unit testing?
I have just began unit testing in angular. 我刚刚开始进行角度单元测试。 I am having a hard time figuring out what should be covered in unit tests in the application.
我很难弄清楚应用程序中的单元测试应涵盖哪些内容。 Should I be testing the below code?
我应该测试以下代码吗?
If I do I will be essentially be passing a valid value of mode
and checking if correct values for various properties of defaultProperties
were emitted by this.propertiesSource
. 如果我愿意,我基本上将传递一个有效的
mode
值,并检查this.propertiesSource
是否为defaultProperties
各种属性发出了正确的值。 Isn't it like writing the same code all over again and more? 难道不像一次又一次地编写相同的代码吗? And this is just one example, application is full of such instances.
这只是一个示例,应用程序充满了此类实例。
If I don't test such things then probably my code coverage will be negligible. 如果我不测试这些东西,那么我的代码覆盖范围可能会忽略不计。
Here is an example of code from component: 这是来自组件的代码示例:
mode: string;
defaultProperties = {
editable: this.editable,
showToolbar: this.showToolbar,
viewMode: this.viewMode,
editMode: this.editMode,
showPopup: this.showPopup,
formSavedClicked: this.formSavedClicked,
cancelClicked: this.cancelClicked,
refresh: this.refresh,
};
private propertiesSource = new BehaviorSubject(this.defaultProperties);
Method to be tested: 测试方法:
setGridMode(mode) {
this.mode = mode;
if (mode === "edit") {
this.propertiesSource.next({
editable: true,
showToolbar: true,
viewMode: false,
editMode: true,
showPopup: false,
formSavedClicked: this.formSavedClicked,
cancelClicked: this.cancelClicked,
refresh: false,
});
} else if (mode == "save") {
this.propertiesSource.next({
editable: false,
showToolbar: false,
viewMode: true,
editMode: false,
showPopup: false,
formSavedClicked: this.formSavedClicked,
cancelClicked: this.cancelClicked,
refresh: true,
})
// more code..multiple if else block like above
}
}
While this may seem somewhat tedious, it is good to unit test everything that you can. 尽管这看起来有些乏味,但是最好对所有可以进行的单元测试。
I would unit test this for a few reasons: 我出于以下几个原因进行单元测试:
If it can be tested, it should be tested. 如果可以测试,则应进行测试。
As Shashank Vivek has indicated, there seems to be a lot of redundancy. 正如Shashank Vivek所指出的那样,似乎存在很多冗余。 Maybe some common functionality can be factored out.
也许可以排除一些常用功能。
Apart from that, the code should certainly be tested, but it does not necessarily have to be done with unit-testing. 除此之外,当然应该对代码进行测试,但是不一定必须通过单元测试来完成。 Possibly integration testing or UI testing are better suited approaches: With unit-testing, as you observe correctly, your tests are likely to just duplicate the code.
可能的是,集成测试或UI测试是更合适的方法:通过单元测试,正如您正确观察到的那样,您的测试很可能只是复制代码。 But, they will probably not be very valuable in the sense that they will not help you to find bugs.
但是,就它们不会帮助您发现错误的意义而言,它们可能不是很有价值。
I assume that these settings are meant to configure the behaviour of some widget library or something similar. 我假设这些设置是为了配置某些小部件库或类似内容的行为。 Certainly you can write a test that checks that for
mode
"edit" the value for refresh
is false
. 当然,您可以编写一个测试来检查
mode
“ edit”的refresh
值为false
。 This test is based on your assumption that this setting will lead to a certain behaviour of that respective widget. 该测试基于您的假设,即该设置将导致相应小部件的某种行为。 But, even if your assumption is wrong, the unit-test will still pass because it simply reflects your own wrong understanding.
但是,即使您的假设是错误的,单元测试仍然会通过,因为它仅反映了您自己的错误理解。
To see if these settings are correct, you therefore will have to perform tests where your code is integrated with the widget library, to see that the setup actually leads to the expected effect - but this is then no longer unit-testing. 要查看这些设置是否正确,因此,您将必须在将代码与窗口小部件库集成的地方执行测试,以查看设置是否确实达到了预期的效果-但这不再是单元测试。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.