r/angularjs • u/mattkentz • Jan 27 '16
Free AngularJS Unit Testing Course
EDIT: The original promotion is over, but you can still get the full course for over 50% off using coupon code REDDIT10 on checkout or using the following link: https://www.udemy.com/unit-testing-angularjs/?couponCode=REDDIT10
Hey everyone, I just launched an AngularJS course on Udemy, covering Unit Testing with Karma, Jasmine and Istanbul.
The first few hundred people can enrol for free using coupon code NGTEST over here: https://www.udemy.com/unit-testing-angularjs/?couponCode=NGTEST
Feel free to ask me questions, and any feedback is appreciated :)
EDIT: The free launch promotion is now over, thanks to all those who enrolled!
53
Upvotes
1
u/dmitri14_gmail_com Jan 28 '16 edited Jan 28 '16
Thanks for the nice tutorial and generosity to put it up for free. Some constructive critique to help make it better:
You should write your tests before not after the code.
Place your test files next to the code to be tested, rather than hiding them away in remote
test/unitdirectory. This is how Angular Material does, as a good example: https://github.com/angular/material/tree/master/src/components/datepicker Also they don't use any preprocessors: https://github.com/angular/material/blob/master/config/karma.conf.jsIn my opinion, you should try to avoid debugging as much as possible, using tests instead to catch all bugs. If you ever need to debug, it might be a sign that your functions are doing too much and need to be split into smaller ones with own tests.
It seems
coverageis a default directory automatically created, so you don't need to configure it manually. This alone does it for me:`
`
Angular-specific:
You should avoid over-using
$scopein favour ofController assyntax.You should keep your controllers thin, abstracting your business logic into Services. It is the Services that should contain most of your tests.
According to more recent practices, you should ever avoid controllers whatsoever :-) Other than having them inside Components. This is what React and Angular 2 do, and you want to make your code structure resembling theirs, even in Angular 1.
You don't need to wrap you directive into
angular.element: https://github.com/angular/material/blob/master/src/components/menuBar/menu-bar.spec.js#L169I've heard that PhantomJS is not quite equivalent to real browsers. Testing in Chrome or browsers you want to support is more reliable, and might be just fast enough for your needs.