Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unit Testing Mock/Stub definitions in Moq

Any reading or advice I've been given on Unit Testing has always suggested a distinct difference between the definition of a Mock and a Stub. My current understanding of these definitions are as follows

Mock: A fake which will be used in your test to make a final assertion

Stub: A fake which will be used in your test to isolate a dependency but not be asserted

However, Moq appears to only allow the creation of Mocks. The Stub namespace in the framework appears to be depreciated with recommendations to use Mock.SetupXXX.

Am I missing something in my understanding of this? Or is there a general understanding that a mock object can infact be used as nothing more that a stub?

Perhaps I am being pedantic, it's just that I have always found language in programming to be very strict and prefer to get my usage of it correct, especially when other developers might be taking over a project.

like image 707
William Avatar asked Sep 08 '10 22:09

William


Video Answer


2 Answers

According to the Moq project site, Moq provides:

Granular control over mock behavior with a simple MockBehavior enumeration (no need to learn what's the theoretical difference between a mock, a stub, a fake, a dynamic mock, etc.)

The lack of distinction between mocks, stubs, and such is a deliberate design decision; A design decision which I, for one, prefer. If I need a true mock, I call Verify() on it. If not, there's no Verify(). I like the simplicity, and I haven't found myself missing the distinction between mock and stub.

like image 87
Eric King Avatar answered Sep 22 '22 16:09

Eric King


Martin Fowler wrote a good article, Mocks Aren't Stubs, which I think makes the distinction clear.

Mocks are used for behavior verification, while stubs supply fake data and normally participate in state verification.

like image 30
Don Roby Avatar answered Sep 22 '22 16:09

Don Roby