For Programmers: Free Programming Magazines  


Home > Archive > Extreme Programming > March 2006 > [Q]Several Questions about Mock Object in Unit Test









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author [Q]Several Questions about Mock Object in Unit Test
Johnny Lee

2006-03-27, 3:58 am

Hi,
I'm new in the study of Mock Object and I found that most examples
from the web are written in Object Oriented language. I wonder whether
we could use Mock Object in C?
Another question, I was told that the differences between Mock
Object and Test Stub is that the Mock Object tests at a finer level of
granularity and it can drive the development of our production code. In
my opinion, the word 'finer' is subjective, how fine is finer? And if
the developing flow is correct, we can also drive the development of
production code by writting test stubs. So, what's the difference
between Mock Object and Test Stub?
Thanks all for your consideration.

Regards,
Johnny

Phlip

2006-03-27, 6:59 pm

Johnny Lee wrote:

> I'm new in the study of Mock Object and I found that most examples
> from the web are written in Object Oriented language. I wonder whether
> we could use Mock Object in C?


http://www.google.com/search?q=mock+object+in+c

Read the first hit!

Now notice that you should write most unit tests, even in C, such that most
tests call live objects, not mock objects. An "object" in C is a struct with
a group of attendant functions that drive it.

(I would not have a problem with driving C code with a C++ test runner.
There might be some technical reason not to.)

> Another question, I was told that the differences between Mock
> Object and Test Stub is that the Mock Object tests at a finer level of
> granularity and it can drive the development of our production code. In
> my opinion, the word 'finer' is subjective, how fine is finer? And if
> the developing flow is correct, we can also drive the development of
> production code by writting test stubs. So, what's the difference
> between Mock Object and Test Stub?


Turn the question around. If you start using TDD, in C, to drive the
development of your production code (meaning if you just start writing
whatever tests work), then the tests themselves will tell you what kind of
mock objects they need.

Most will just need "Fake Data", where you stuff a struct with values you
know the production code should use, then pass the struct to production
code, then sample the results back.

What's more important than the Mock question is whether your code is
sufficiently decoupled that a unit test can get near it and can intercept
its intermediate values. So I would suggest you should start experimenting
with a C Unit Test rig (OCunit, or CUT, etc.), instead of worrying about the
high-end details! You might have a case of Analysis Paralysis, and that's
what unit tests help prevent.

--
Phlip
[url]http://www.greencheese.org/ZLand[/url] <-- NOT a blog!!!


Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com