In Angular everything seems to have a steep learning curve and unit testing an Angular app definitely doesn't escape this paradigm.
When I started with TDD and Angular I felt that I was spending twice (maybe more) as much time figuring out just how to test and maybe even more just getting my tests set up correctly. But as Ben Nadel put it in his blog there are ups and downs in the angular learning process. His graph is definitely my experience with Angular.
However as I have progressed in learning Angular and unit testing as well, now i feel that I am spending much less time setting up tests and much more time making tests go from red to green - which is a good feeling.
So I have come across different methods of setting up my unit test to mock services and promises and I thought I would share what I have learned and also ask the question of:
Are there any other or better ways of accomplishing this?
So onto the code, that what we all come for here anyways - not to listen to some guy talk about his love, err accomplishments learning a framework.
This is how I started out mocking my services and promises, I'll use a controller, but services and promises can be mocked in other places obviously.
describe('Controller: Products', function () {
var//iable declarations
$scope,
$rootScope,
ProductsMock = {
getProducts: function () {
} // There might be other methods as well but I'll stick to one for the sake of consiseness
},
PRODUCTS = [{},{},{}]
;
beforeEach(function () {
module('App.Controllers.Products');
});
beforeEach(inject(function ($controller, _$rootScope_) {
//Set up our mocked promise
var promise = { then: jasmine.createSpy() };
//Set up our scope
$rootScope = _$rootScope_;
$scope = $rootScope.$new();
//Set up our spies
spyOn(ProductsMock, 'getProducts').andReturn(promise);
//Initialize the controller
$controller('ProductsController', {
$scope: $scope,
Products: ProductsMock
});
//Resolve the promise
promise.then.mostRecentCall.args[0](PRODUCTS);
}));
describe('Some Functionality', function () {
it('should do some stuff', function () {
expect('Stuff to happen');
});
});
});
For us this worked, but as time went on I thought there must be a better way. For one I hated the
promise.then.mostRecentCall
thing, and if we wanted to reinitialise the controller then we had to pull it out of the beforeEach block and inject it individually into each test.
There has to be a better way...
Now I ask does anyone have other ways to set tests up, or and thoughts or feeling on the way I have chose to do it?
See Question&Answers more detail:
os