Loading
Current section: Test Structure 6 exercises
Problem

Imperative vs. Declarative Assertions

Transcript

00:00 Let's start with the assertions. I'm talking about this little guy. Assertions are easily the most useful part of any test. In fact, all the other test phases exist just so you can write the assertion the way you want. Remember how you learned that tests are about describing the intention behind the code? Well, assertions are precisely the means of how you can do that.

00:18 The way we have written them as of now, though, has a few problems. First of all, they're imperative. This means that they concern themselves too much with how they work. All this if statements, comparison, throwing an error, these have a place to be, but they don't have to surface as much in the test. Second problem, take a look at this, another test we added for the congratulate function.

00:37 Here's another simple assertion, but it takes way too much space. Another four lines of code. Imagine writing dozens, hundreds of this. The test file will grow out of scope very quickly. Wouldn't it be nice if we could write the same assertion in a more declarative way like this? So we say that we expect the result of the greet function to be hello, John.

00:56 Notice how this reads and feels just much nicer. But you may be wondering, where does this expect function comes from? You guessed it. You will be implementing it as a part of this exercise. So have fun and see you when you're done.