Beginning AngularJS

(WallPaper) #1
Chapter 10 ■ Deployment ConsiDerations

// We expect the kids array to contain the string "Natalie"
// If it doesn't the test fails
var kids = ["Jenna", "Christopher", "Natalie", "Andrew", "Catie"];
expect(kids).toContain("Natalie");


// We expect 10 to be less than 15.
// If it isn’t the test fails
expect(10).toBeLessThan(15);


Here we have five expectations, each with a corresponding matcher. Hopefully, you can see how expressive this
is. It’s not as difficult as you might think either; it doesn’t take much work to write a test that will pass if, and only if,
one plus one equals two. It was also quite easy to fail a test if a specific value is missing from an array. That being said,
there is a lot more to know about unit testing and, even when you know it, doing it right can be quite an art form.
While we don’t get chance to dive into testing fully in this book, I strongly encourage you to consider exploring
the topic further. For me, writing tests with a good testing framework, and writing them up front, is a great way to get a
sense that my application is watertight. When I change my application, I can run my tests again and make sure that it
is still in good shape (and fix it if it isn’t).
While I have focused on unit testing in this section, there are, of course, many other kinds of testing that you
should consider conducting. Black box or end-to-end testing has its place, as does Write Behind Testing. I encourage
you to make the time to dig deeper. Believe it or not, testing can actually be quite enjoyable and rewarding.


■ Tip to find out more about Jasmine, you can visit http://jasmine.pivotallabs.com/. other unit test frameworks


include, but are certainly not limited to, qUnit (http://qunitjs.com/) and mocha (http://visionmedia.github.io/mocha/).


personally, i find it hard to pick a favorite from these, as they are all exceptionally good.


Error Handling


It can be tricky for developers who are new to JavaScript frameworks to figure out the best way to perform error handling;
far too often we see applications that really don’t have any strategy at all for doing so. As with most other topics in this
chapter, we don’t really get to dive into a lot of detail, but a few pointers will hopefully prompt you to consider developing
some sort of error handling strategy. Fortunately, error handling is mostly about common sense and discipline.
First and foremost, you must have some kind of strategy in place. While it might be okay for debugging purposes,
it really isn’t okay to deploy your application with code such as that shown in Listing 10-8.


Listing 10-8. Lazy error handling


if(user.paySuccess()){
// The users payment was succesful
goToSuccessScreen();
}
else{
// The users payment failed, pop up an alert
// and log to the console.
alert('An error has occured');
console.log('An error has occured');
}

Free download pdf