4 ms·
[pedantic mode on] In this particular case, or at least the first test case, the unit test doesn't actually need the actual $location service; a mock will suffi
by Cthulhu_ 13y ago
[pedantic mode on]
In this particular case, or at least the first test case, the unit test doesn't actually need the actual $location service; a mock will suffice to set the expectation (i.e. the path method returning a path)
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 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]
- _mikz 13y agoIn this case it might not be needed, but some controllers have calls that are made on initialisation (like NgResource) which makes harder to use $httpBackend (you have to mock all calls if you want to verify that no calls were left at the end). I took the article more like best-practice for unit testing controllers. But truth to be told, it would be nicer if author would explain why to initialise controller in a method etc.