AngularJS Essentials
Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
Unit Testing<br />
Have you ever stopped to think about how much time we've spent just to<br />
understand and reproduce a defect? After this, we need to spend even more time<br />
looking for, between thousands of lines, the exact piece of the code that is causing<br />
this defect. Many times, the fastest, and perhaps the easiest step is fixing it. And<br />
what about manually testing the code repeatedly? Every time anything is changed,<br />
you need to test it again. However, as the nature of software is all about changing,<br />
test automation should be considered as an important long-term investment that will<br />
support a sustainable pace and also improve the quality of each release.<br />
Talking about quality, tests are the seeds of quality. Without that, nobody would<br />
have enough confidence, or even courage, to improve the existing code by<br />
refactoring it more often. Therefore, we could accumulate too much technical debt,<br />
affecting the productivity and also bringing down the motivation of the team.<br />
In the case of JavaScript, it is a very dynamic language that provides a strong<br />
combination of both functional and object-oriented paradigms. However, despite<br />
the advantage of being interpreted easily, JavaScript comes with a risk. The lack of a<br />
compiler may lead you to introduce many syntax errors such as unknown variables<br />
or function names, missing semicolons, and many others.<br />
Beyond tools such as JSLint and JSHint that verify our code by looking for the most<br />
common syntax errors, there are many testing frameworks available for JavaScript.<br />
We are going to use Jasmine, which is easy to use and also has a great community<br />
and support.<br />
In order to test the <strong>AngularJS</strong> components, we'll use a module called ngMock that<br />
supports dependency injection and also comes with a mocking mechanism.<br />
www.it-ebooks.info