What is the meaning of the term “Mocking” in C#? [closed]

北战南征 提交于 2019-12-13 23:25:09

问题


What does the term mocking mean in the context of C#/WPF?


回答1:


In the context of unit testing, mocking is providing a fake implementation of a type that your code under test interacts with.

See http://en.wikipedia.org/wiki/Mock_object

The book "The Art of Unit Testing" by Roy Osherove has good explanations and I recommend it if you are getting started with unit testing in C#

A mock differs from a stub in that it verifies the interaction with your code under test. A stub simply returns pre-defined values to help unit test your code.




回答2:


See!! I will give you some simple examples that makes you to understand.

  1. Suppose there is a method in class say ParseEBCDICFileToASCII(strig fileName) and this file also validates if file exist or not File.Exist(). Now, you are writing unit test for parsing logic and you are providing some file path in ParseEBCDICFileToASCII method then this method will always fail or return back because what ever the file you gave that does not exist in reality. In this case you will mock the File.Exist() in such a way that, whenever this will call you are going to return a fake/mocked value. So in unit test, your logic for File.Exist will always return the mocked value.

  2. Also there are cases when you are writing test for client application for any web service then for unit test you don't need actual web service. You can write a mocked web service and your actual logic will always hit to mocked service the return mocked value.

So mocking is a way of returning the fake values so that you can more concentrate on testing your actual logic...



来源:https://stackoverflow.com/questions/17568516/what-is-the-meaning-of-the-term-mocking-in-c

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!