简体   繁体   English

单元测试与Express.js应用程序的集成测试

[英]Unit testing vs Integration testing of an Express.js app

I'm writing tests for an Express.js app and I don't know how to choose between unit tests and integration tests. 我正在为Express.js应用程序编写测试,我不知道如何在单元测试和集成测试之间进行选择。

currently I experimented with: 目前我尝试过:

unit tests - using Sinon for stubs/mocks/spies and Injects for dependency injection to modules. 单元测试 - 使用Sinon进行存根/模拟/间谍,使用Inject进行模块的依赖注入。 with this approach I have to stub MongoDB and other external methods. 用这种方法我必须存根MongoDB和其他外部方法。

I thought about unit testing the individual routes and then using an integration test to verify that the correct routes are actually invoked. 我考虑过对单个路由进行单元测试,然后使用集成测试来验证是否实际调用了正确的路由。

integration tests - using Supertest and Superagent, much less code to write (no need to mock/stub anything) but a test environment should exist (databases, etc..) 集成测试 - 使用Supertest和Superagent,编写更少的代码(不需要模拟/存根任何东西),但应该存在测试环境(数据库等......)

I'm using Mocha to run both styles of tests. 我正在使用Mocha来运行两种测试方式。

how should I choose between those two different approaches ? 我应该如何选择这两种不同的方法?

You should probably do both. 你应该两个都做。 Unit test each non-helper method that does non-trivial work. 单元测试每个执行非平凡工作的非辅助方法。 Run the whole thing through a few integration tests. 通过一些集成测试来运行整个过程。 If you find yourself having to do tons and tons and tons of mocks and stubs, it's probably a sign to refactor. 如果你发现自己不得不做吨,吨和吨的模拟和存根,这可能是重构的标志。

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

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