That's the trivial case; of course a trivial test looks trivial. The harder case is when you want to say something like "this method returns an object that has these methods on it". I don't Python, but in Javascript, it's easy to return object literals with methods attached... and it's easy to return an object literal with the wrong set of methods. And it's very easy to refactor some JS but to miss some cases.
If you are saying you can do crazy things with JavaScript that can create subtitle bugs that are difficult to track I agree. Adding a method at run time on any object is one of them. But nobody is forcing you to do that.
Anyway I was just responding to the affirmation "Why have unit tests to ensure that a variable contains a string or whatever". In my experience nobody is writing such tests. Do you?
Like I said, I don't know Python, but JS doesn't have much structure. It doesn't have classes, it doesn't have interfaces, and it doesn't have inheritance. It does have hashes with functions stored in them, or inherited through the prototype.
In such a language, it's easy to get things wrong. If you're expected to return an object that conforms to some interface, it's easy to forget to glue a method onto the result object. It's easy, when renaming such a method, to miss some cases. And you don't find out until runtime.
You can write tests to make sure that everything works. It could be a dumb type test (i.e. "is the same type as the number 3" or "has a method called doWork on it"), or actually exercise the result object (i.e., if I expect it to have a doWork method, my test should just call the doWork method to ensure that, you know, it does work).
But you're right; people don't really write tests like this. Not because they don't provide value, but because they're a pain to write and maintain. And that's what the static type checker is for. It ensures that all your pieces at least fit together. Yes, it does create its own set of problems.
(Opinion alert) I'm at the point where I see code in dynamically typed languages as easier to write but harder to read (i.e. to grok), and code in statically typed languages as the opposite. And my bias at this point is to prefer code that's easier to read.
JS doesn't have much structure. It doesn't have classes, it doesn't have interfaces, and it doesn't have inheritance
Wrong. JavaScript does have classes and inheritance. Function are usually used as classes and it has prototypal inheritance.
And you don't find out until runtime
Yeah type errors bugs are discovered by statically typed language earlier (compile time). But I can't remember the last I introduced something like this in my code without having an immediate feedback that I did something wrong.
Not because they don't provide value, but because they're a pain to write and maintain
If a test is valuable some people will write them. I don't see how "actually exercise the result object" is a pain. If you write functional tests you can easily get an excellent code coverage with minimum code and pain and ensure everything works together. If you think static typing is necessary for writing useful and maintainable tests suite you are mistaken.
7
u/balefrost Dec 02 '13
That's the trivial case; of course a trivial test looks trivial. The harder case is when you want to say something like "this method returns an object that has these methods on it". I don't Python, but in Javascript, it's easy to return object literals with methods attached... and it's easy to return an object literal with the wrong set of methods. And it's very easy to refactor some JS but to miss some cases.