I'm stuck for hours for this and it's because I'm new to Jasmine and Angularjs. Basically, I have this controller.
angular.module('toadlane.controllers', []).
controller('MainController', function($scope, $http, SomeService) {
var promise = SomeService.getObjects();
promise.then(function(data) {
if(data.length > 0) {
$scope.objs = data;
} else {
$scope.message = "Please come back soon";
}
});
});
How can I write a Jasmine test to mock SomeService to stub out data and simulate if there's data the scope's length should not be 0 and if data is [] the message should be "Please come back soon".
'use strict';
describe('controllers', function () {
var scope, someService;
beforeEach(module('services'));
beforeEach(module('controllers'));
beforeEach(inject(function (SomeService) {
someService = SomeService;
var callback = jasmine.createSpy();
someService.getObjs().then(callback);
}));
it('should return some objs', inject(function ($rootScope, $controller) {
scope = $rootScope.$new();
expect($controller('MainController', {$scope: scope, SomeService : someService})).toBeTruthy();
expect(scope.objs.length).toEqual(2);
}));
it('should return no objs', inject(function ($rootScope, $controller) {
scope = $rootScope.$new();
expect($controller('MainController', {$scope: scope, SomeService : someService})).toBeTruthy();
expect(scope.message).toEqual("Please come back soon");
}));
});
The tests are not finished because I don't know how to stub then from promise
Any help is very appreciated. Thanks.
Defers can be tested by mocking promises with functions which return object with chained methods. In this case mocked SomeService.getObjs should return an object with method then.
In order to do this add another one beforeEach statement:
beforeEach(module(function ($provide) {
$provide.value('SomeService', {
getObjs: function () {
return {
then: function (callback) {
// mockedResponseData is passed to callback
callback(mockedResponseData);
}
};
}
}));
Mocked service will immediately load result in existing promise callback function.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With