How to Unit Test Controllers In AngularJS Without Setting Your Hair On Fire
nathanleclaire.com
nathanleclaire.com
In the article's scraping example, a service that works like `Scrape.url('success.com')` could be mocked and sidestep the $http boundary with its flushing messiness. It also would make the system easier to reason about.
For more of this line of thinking take a look at the "Don't mock what you don't own" guideline which I've found very useful.
For clarity, I'd pull the createController function out of the beforeEach method. Actually, in this case you don't even need to have it as a separate method; simply calling $controller will be enough, since one, the controller does not have any behavior on initialization (besides adding a function to the scope), and second because all public methods are assigned to the scope.
Third, the injector can be skipped in favor of just having the appropriate service names in the call to inject.
I took the liberty to clean up as I'd do it: http://pastebin.com/Vv63J3P5
Same applies to the second unit test. I also wouldn't use the $http service directly from a controller, or mock out http calls using $httpBackend; either use a service that wraps $http calls itself, or ngResource/$resource. Those two can then easily be mocked out in the controller spec (slurpService.scrape(url).andReturn(neatlyParsedResult)) [/pedantic]
So we just pass our own mocks in, it's much simpler than trying to use Angular's injector mechanism.
The only thing that is more or less difficult about testing Angular ist the boilerplate one has to write to instantiate everything correctly. When writing the code, Angular handles the bootstrapping of the application, but in the tests one has to do it manually.
What's positive about it is that one learns how Angular works internally. I would even say that doing TDD with Angular is actually a lot of fun.