What's the difference between a stub and mock?
2013-07-03 21:48
471 查看
I believe the biggest distinction is that a stub you have already written with predetermined behavior. So you would have a class that implements the dependency (abstract class or interface most likely) you are faking for testing purposes and the methods would just be stubbed out with set responses. They wouldn't do anything fancy and you would have already written the stubbed code for it outside of your test.
A mock is something that as part of your test you have to setup with your expectations. A mock is not setup in a predetermined way so you have code that does it in your test. Mocks in a way are determined at runtime since the code that sets the expectations has to run before they do anything.
Tests written with mocks usually follow an initialize -> set expectations -> exercise -> verify pattern to testing. While the pre-written stub would follow an initialize -> exercise -> verify. The purpose of both is to eliminate testing all the dependencies of a class or function so your tests are more focused and simpler in what they are trying to prove.
I hope that helps.
reference:
Mocks Aren't Stubs, posted by Martin Flower,2007.
What's the difference between a mock & stub? ,stackoverflow
The art of unit testing.
A mock is something that as part of your test you have to setup with your expectations. A mock is not setup in a predetermined way so you have code that does it in your test. Mocks in a way are determined at runtime since the code that sets the expectations has to run before they do anything.
Tests written with mocks usually follow an initialize -> set expectations -> exercise -> verify pattern to testing. While the pre-written stub would follow an initialize -> exercise -> verify. The purpose of both is to eliminate testing all the dependencies of a class or function so your tests are more focused and simpler in what they are trying to prove.
I hope that helps.
reference:
Mocks Aren't Stubs, posted by Martin Flower,2007.
What's the difference between a mock & stub? ,stackoverflow
The art of unit testing.
相关文章推荐
- What's the Difference Between Iterators and Generators in Python
- What's the difference between jquery.js and jquery.min.js?
- What's the Difference between the frame and the bounds?
- What's the difference between returning void and returning a Task?
- What's the difference between Unicode and UTF-8?
- what's is the different between git rebase and git meger.
- What is the difference between dpkg and aptitude/apt-get?
- What’s the difference between Taxonomies and Ontologies? - Ask Dr. Search
- The difference between SendMessage and PostMessage
- What is the difference between JRE,JVM and JDK?
- In SQL, what is the difference between a left join and a left outer join?
- What's the difference between tf.placeholder and tf.Variable
- What’s the Difference between a Hue, Tint, Shade and Tone ?
- What are the differences between WebAPI and WebAPI 2
- What is the difference between new and malloc()?
- What is the difference between a processor, a chip, a socket, and a core?
- What is the difference between User Control and Custom Control?
- Visual Studio中“生成”与“重新生成”的区别(The difference between "build" and "rebuild")
- What is the difference between angle bracket < > and double quotes " " while including header files