I frequently have a situation where I want to test a proc that calls
another proc of mine. How do you "stub out" the procs inside the proc
being tested?
For example:
proc bar{} {
# ... rc gets set somewhere
return $rc
}
proc foo{} {
# lotsa code
set var [bar]
# ...use "var" for further processing...
}
Using tcltest, I want to test "foo". But, I want "bar" to return a
specific value so I can verify different logic paths. The most obvious
way would be something like this:
::tcltest::test foo.1 {
Testing foo
} -setup {
proc bar{} {return 99}
} -body {
foo
} -cleanup {
} -result 99
But, doing it this way will destroy the definition for "bar" and make
it tough to use it in subsequent tests. Put another way, how can I use
the "bar" proc (or the "foo" proc for that matter) in various places,
but not step all over my own code?
How is this problem normally handled?
TIA,
-T
Try:
::tcltest::test foo.1 {
Testing foo
} -setup {
rename bar _bar
proc bar {} {return 99}
} -body {
foo
} -cleanup {
rename _bar bar
} -result 99
--
+--------------------------------+---------------------------------------+
| Gerald W. Lester |
|"The man who fights for his ideals is the man who is alive." - Cervantes|
+------------------------------------------------------------------------+
No, but it is most likely the easiest.
Other ways: load your code to be tested into a safe interp with aliases
defined the way you want them.
Instead of using rename, look and remember the procedure definition then
redefine it at the end.
Do you have a problem with rename?
It just seems inelegant. But if that's the preferred method, then I'll
do that. :)
Thanks Gerald.
-T