简体   繁体   English

JavaScript单元测试。 怎么样?

[英]JavaScript Unit Test. What to mock?

I'm trying to introduce Unit Test in my development process and I'm not sure how to do it. 我正在尝试在我的开发过程中引入单元测试,我不知道该怎么做。

I have a big application with a lot of modules ( separated js files ). 我有一个很大的应用程序,有很多模块(分开的js文件)。 For example: 例如:

  • videoInfo.js videoInfo.js
  • availableVideos.js availableVideos.js
  • availableChannels.js availableChannels.js
  • networkSettings.js networkSettings.js
  • ....... .......

There are also a few modules that provide services for the rest: 还有一些模块为其余模块提供服务:

  • eventHandler.js eventHandler.js
  • i18n.js i18n.js
  • ajax.js ajax.js
  • configuration.js configuration.js
  • auth.js auth.js
  • baseModule.js baseModule.js
  • ........ ........

The question is: how do I test a module like videoInfo.js that uses all the services modules? 问题是:如何测试像使用所有服务模块的videoInfo.js这样的模块? I see two approaches: 我看到两种方法:

  • I load only videoInfo.js and mock all the internal interfaces. 我只加载videoInfo.js并模拟所有内部接口。 The problem with this is that I won't catch errors if any of the internal interfaces change. 这样做的问题是,如果任何内部接口发生变化,我将不会发现错误。
  • I load all modules (the whole app) and test the videoInfo with the real app environment ( except the external AJAX calls that will be simulated) 我加载所有模块(整个应用程序)并使用真实的应用程序环境测试videoInfo(除了将要模拟的外部AJAX调用)

What is the good approach to do this when you have a lot of internal dependencies? 当你有很多内部依赖关系时,这样做的好方法是什么?

Ideally, you want both of your approaches in your test suite. 理想情况下,您需要在测试套件中使用这两种方法。

You want to mock all of the dependencies (in your example for videoInfo.js) so that you can isolate its code from anything else and be sure that it is working correctly. 您想要模拟所有依赖项(在您的videoInfo.js示例中),以便您可以将其代码与其他任何内容隔离,并确保它正常工作。 Depending on the design of the code that you have, this can be difficult. 根据您拥有的代码设计,这可能很困难。 As the code may not have been written with testing in mind and so won't lend itself to mocking. 由于代码可能没有考虑到测试,因此不适合嘲笑。

You would also want to have an entire app level series of tests that make sure that all the interactions between the parts will work correctly. 您还希望拥有一整套应用程序级别的测试,以确保部件之间的所有交互都能正常工作。 Depending on the app, these tests can be very brittle due to dependence on data that you have no control over. 根据应用程序的不同,由于依赖于您无法控制的数据,这些测试可能会非常脆弱。

These tests would likely be in separate suites so that they can be run independently of each of other. 这些测试可能位于不同的套件中,因此它们可以独立于其他套件运行。

If you are just getting started in writing tests for code, write the best test that you can. 如果您刚开始编写代码测试,请尽可能编写最佳测试。 Tests are code and should be refactored as you go along. 测试是代码,应该随着你的进行重构。 Write the easiest test that you can. 写出最简单的测试。 Writing tests for legacy code is extremely difficult. 为遗留代码编写测试非常困难。 The current design may be difficult to test properly. 目前的设计可能难以正确测试。 So when you are making your changes do what you can. 因此,当您进行更改时,尽可能做到。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM