Imagine I have function foo()
:
var foo = function(string) {
return string.replace(/a/g, '');
};
I have following tests for it to work:
foo()
exists;foo()
stripsa
's from string and does nothing to string withouta
's;foo()
throwsTypeError
if given something else than string;
The problem is with test #3 — however it's green from the beginning, it's not my merit. I expect to have written something like this:
var foo = function(string) {
if (typeof string !== 'string') {
throw new TypeError('argument should be a string');
}
return string.replace(/a/g, '');
};
but I can't, because there is no test for it. So foo()
really throws TypeError, but not because of argument of wrong type, but because null, undefined, number, array, boolean, regexp etc. objects given as argument don't provide replace()
method.
I think I need this test, just because JS team may for example change TypeError
for this particular case to something like MissingMethodError
, but I will violate Red > Green > Refactor principle. How should I resolve this situation?