C#-单元测试,模拟吗? [英] C# - Unit test, Mock?

查看:79
本文介绍了C#-单元测试,模拟吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

应该使用目标类的内置单元测试生成器(VS),还是应该学习如何编写单元测试?这是什么模拟"的东西?我一遍又一遍地听到它,但是没人在乎给出很好的解释.

The builtin unit test generator(VS) for the target classes should that be used or should I learn myself how to write a unit test? And whats this "Mock" thing? I hear it over and over but none cares to give a good explanation.

谢谢.

推荐答案

您需要学习自己编写单元测试.从正确的角度开始,对很多人在以下方面会犯错误的术语有很好的理解:

You need to learn to write unit tests on your own. Start off on the right foot with good comprehension of terminology that many people make mistakes regarding:

单元测试:测试单个代码单元,非常小的原子测试.

Unit test: Testing a single unit of code, very small atomic test.

集成测试:测试集成在一起的多个代码单元,以遍历不同的层并确保它们彼此正确使用.这些应该在单元测试已验证各个单元独立工作之后进行.许多人错误地将这些称为单元测试.

Integration test: Testing multiple units of code integrated together, to go through the different layers and ensure they are using eachother correctly. These should be done after unit tests have verified the individual units work independently. Many people mistakenly refer to these as unit tests.

内置验证测试:通过部署内置产品并运行将以用户方式与之交互的测试来测试内置产品.也经常被误称为单元测试.这些是功能最齐全的最大型测试,通常只是由测试团队手动完成,而不是自动化.

Built Verification Test: Testing the built product by deploying it and running tests that will interact with it in the manner a user would. Also mistakenly referred to as unit tests frequently. These are the largest most full featured tests and often just done manually by testing teams rather than being automated.

这是MOQ的快速入门,它是一个模拟框架: https://github.com/Moq/moq4/wiki/Quickstart

Here's the quickstart for MOQ which is a mocking framework: https://github.com/Moq/moq4/wiki/Quickstart

模拟是将可能依赖于其他内容的一小段代码进行模拟,然后模拟其他内容,以便您可以控制要测试的代码段周围的情况.

Mocking is the act of taking a small piece of code that may depend on other things, mocking those other things up so that you can control the circumstances surrounding the piece of code you want to test.

模拟的目的是测试中的原子性.它使您可以仅测试所需的单个代码,而不会由于依赖的代码段中的错误而影响测试.此外,模拟还使您能够构造各种方案来测试每段代码的边缘情况.

The purpose of mocking is atomicity in tests. It allows you to test just the individual piece of code you want, without having it's tests affected due to bugs in dependent code pieces. Also mocking gives you the ability to fabricate a variety of scenarios to test the edge cases of each piece of code.

模拟通常用于在单元测试中为目标代码创建边界,尽管在集成测试中也经常使用它来制造用作目标代码的种子的资源.

Mocking is generally purposed for creating boundaries around the target code in unit tests, though it's not uncommon to use it in integration tests too for fabricating a resource that acts as the seed to the integrated code chain you're targeting.

这篇关于C#-单元测试,模拟吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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