Reputation: 540
I have following code in my controller and I want to write Jasmine test case for this part. I tried to write one, but its throwing following error TypeError: Object [object Array] has no method 'then'
Controller Code ::
$scope.doGetList = function() {
var queryString = {......sending some query parameters};
searchResource.getList(queryString).then(
function (data) {
$scope.sugesstions = data;
}
);
};
Jasmine Test Case ::
it("should return provided list", angular.mock.inject(function($rootScope, $controller) {
var scope = $rootScope.$new();
var searchResource = {
getList: function() {
return ['suggestions1', 'suggestions2', 'suggestions3'];
}
};
$controller(
headerController,
{
$scope: scope,
cartsService: null,
currentUser: null,
searchResource: searchResource
}
);
expect(scope.sugesstions).toBeDefined();
expect(scope.sugesstions.length).toBe(0);
//this method will call mock method instead of actual server call
scope.doGetAutocomplete();
expect(scope.sugesstions.length).toBe(3);
expect(scope.sugesstions[0]).toEqual('suggestions1');
expect(scope.sugesstions[1]).toEqual('suggestions2');
expect(scope.sugesstions[2]).toEqual('suggestions3');
}));
How should I write it.
Upvotes: 0
Views: 756
Reputation: 446
You'd have to wrap async call in runs(). From jasmine doc: http://pivotal.github.io/jasmine/#section-Asynchronous_Support
Or, I use jasmine-as-promised with better support: https://github.com/ThomasBurleson/jasmine-as-promised
Upvotes: 1