0

I have this odd situation in unit tests when I have to check function that does return value which can't be made manually (value). In fact this function is made for creating this value. So test like this (javascript and QUnit framework):

asyncTest("UTF-8 - ArrayBuffer conversion", 1, function() {
    var arrayBuffer;
    var testUtf8 = "łużyński";
    var blob = new Blob([testUtf8]);
    var f = new FileReader();
    f.onload = function(e) {
        arrayBuffer = e.target.result;
        start();
    };
    equal(utf8ToArrayBuffer(testUtf8), arrayBuffer, "UTF-8 to ArrayBuffer, OK!");
});

Is redunant, because preparing arrayBuffer variable to compare it with returned value is actually the same as using utf8ToArrayBuffer(testUtf8), so test would look like this:

equal(utf8ToArrayBuffer(testUtf8), utf8ToArrayBuffer(testUtf8), "UTF-8 to ArrayBuffer, OK!");

Should I test things like this or leave it? Or maybe there is another approach to this?

Jason Evans
  • 28,906
  • 14
  • 90
  • 154
Rafał Łużyński
  • 7,083
  • 5
  • 26
  • 38

1 Answers1

0

You should not test that two equal function calls return the same. You should test that the result of the function call is what you expected. In your case this means that you have a value ("łużyński") as input, and you get an array buffer as output. You could test if the input value actually is contained in the output.

But without the code of function to test (utf8ToArrayBuffer()), it's hard to say what exactly you could test.

Odi
  • 6,916
  • 3
  • 34
  • 52
  • Well, I wrote that preparing value to compare is the same as function I'm testing. However, implementation shouldn't matter, because this is TDD, and I have to write tests first. All I know right now is that `utf8ToArrayBuffer(testUtf8)` has to return arrayBuffer with my value converted. Now I have to check somehow if returned value is OK. I can convert this arrayBuffer back to text but then I'll have to use another function `arrayBufferToUtf8` which has identical problem with tests. So it would be something like redundant cross-testing. – Rafał Łużyński Feb 15 '13 at 17:22
  • For me this makes perfect sense, because the code of both functions can change independently. But as it is such a small function, you should not worry about it. If you want to test more than just the return value, you could consider to use a mock and see if it gets manipulated the right way. – Odi Feb 15 '13 at 17:49