问题
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.
Suppose there is a method in class say
ParseEBCDICFileToASCII(strig fileName)
and this file also validates if file exist or notFile.Exist()
. Now, you are writing unit test for parsing logic and you are providing some file path inParseEBCDICFileToASCII
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 theFile.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 forFile.Exist
will always return the mocked value.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