Testing promises returned by getList()
See original GitHub issueI’m trying to write unit tests for a controller that uses
$scope.users = Restangular.all('user').getList();
But when I test the value of $scope.users, it’s
{"restangularCollection":true}
I’m not really familiar with promises, but I think this has to do with the fact that the return value hasn’t been applied to the scope yet, since if I set $scope.users
in the success callback it works fine.
Restangular.all('user').getList()
.then(function(users) {
$scope.users = users;
});
I made a gist with a more complete example.
Issue Analytics
- State:
- Created 10 years ago
- Comments:7 (1 by maintainers)
Top Results From Across the Web
How to unit test a function that returns a promise in angular
it('test getHeaderData()', async () => { const serviceSpy: Service = TestBed.get(Service); SpyOn(serviceSpy, 'getList').and.
Read more >Unit Testing Async Calls and Promises with Jasmine - Medium
This post will show you a simple approach to test a JavaScript service with an exported function that returns a promise. Unit testing...
Read more >getList JavaScript and Node.js code examples - Tabnine
test /queue.test.js/it. it('should return completed jobs', function() { queue = createQueue({}, [ createJob(true), createJob(true), createJob(false, ...
Read more >Async / Await returning Promise <pending> instead of values
Hi, There seems to be an issue with Async/Await handling. Code below: async function getData() { console.log('logging'); const test = await ...
Read more >Solved: Value from a promise object? - Qlik Community
function getHTMLcode() { return new Promise(function(resolve, reject) { app.getList('FieldList').then(function (result) { //here the promise ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
‘use strict’; angular.module(‘myApp’, [“restangular”])
describe(‘Controller: UserCtrl’, function () { beforeEach(module(‘myApp’));
});
Hey,
The sugar of
$scope.users = a.getList()
is added when you use a promise in a template. What it actually does is add a$$v
property which once the promise is fullfilled, it’s set to the value, so the DOM watches for changes on that property. While waiting it’s null, then it’s fullfilled.That will work with templates. If you try to get it to work with something else, it won’t.
You can still use the sugar on the Controller, but on the test, to check if everything is OK, you’ll need to do
$scope.users.then(function(users) {...}
and in there check, but you can still keep the sugar on the controller. Hope it helps!