DEV Community

Discussion on: What dev topic do you think you should understand, but don't?

Collapse
 
jacoby profile image
Dave Jacoby

Each language has a mechanism by which you can write function foo(), so that let bar = foo("bar") and you can test bar = "blee" and fail if it doesn't.

And, by each language, I am including bash shell scripting. There, it's called bats.

What I don't get is how to write functions so that they're testable like that. I see the extremes of let x = make_an_h1_tag("test") ; if ( x != "<h1>test</test>" ) { console.log("WRONG!") and cases where you have to stub a DB driver and two calls to REST APIs.

Once I have a sense of what's reasonable to test, I can use the tools available to write decent ones, but right now, I don't have that.

Collapse
 
rhymes profile image
rhymes

One good advice I can give you is to never test the library (unless you don't really trust it, but then you should probably change library :D).

So, if have a pure fuction that adds two numbers, your test should make sure that by giving 3 and 2 you get 5, maybe also test what happens with 0, negative numbers, floating numbers and if you want to feel sure, see how it behaves with something that's not a number (if you are operating with a loosely typed language).

If your function calls a database and a REST API the first thing you have to ask yourself is: am I here to test the database and the API or the function itself? I guess in this case you're trying to test the function. So DB and API should be black boxes in this case. You should stub those two, make the stubs return reasonable values and see what happens to your own function.