Java Reference
In-Depth Information
This is often referred to as the “red-green-refactor” cycle of TDD, as failing tests usually
show up as red and tests that pass show as green.
Testing Frameworks
It is possible to write your own tests, as we've seen, but this can be a laborious process.
Testing frameworks provide a structure to write meaningful tests and then run them. There
are a large number of frameworks available for JavaScript that can be seen on Ahref
magazine. We'll be focusing on the Jasmine framework by Pivotal Labs, which was origin-
ally known as JsUnit.
Jasmine
Jasmine is one of the most popular JavaScript TDD frameworks. It uses the concept of
specs , which are short descriptions of what the code should do. A typical spec looks like
this:
describe("The squareRoot function", function() {
var number;
it("square roots 4", function() {
answer = squareRoot(4);
expect(answer).toBe(2);
});
});
This spec is made up of a describe function and a function called it , which contains the
actual spec. There's another function called expect that's used for the actual tests. There
are also some thoughtfully named functions called matchers. One example is toBe() ,
seen in the previous code sample. These are placed one after the other so that they read like
an English sentence, making them easier to understand (even for non-programmers) and
the feedback they provide more meaningful. It's important to recognise that they are just
functions at the end of the day, so they behave in exactly the same way as any other func-
tion in JavaScript. This means that any valid JavaScript code can be run inside the spec.
 
Search WWH ::




Custom Search