question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Testing promises returned by getList()

See original GitHub issue

I’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:closed
  • Created 10 years ago
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
Dashuecommented, Sep 29, 2013

‘use strict’; angular.module(‘myApp’, [“restangular”])

.controller('UserCtrl', function ($scope, Restangular) {
    Restangular.all('user').getList().then(function(users) {
        $scope.users = users;
    });
});

describe(‘Controller: UserCtrl’, function () { beforeEach(module(‘myApp’));

var UserCtrl;
var scope;
var httpBackend;

// Initialize the controller and a mock scope
beforeEach(inject(function ($controller, $rootScope, $httpBackend) {
    httpBackend = $httpBackend;

    var fauxUsersList = [ 'foo' ];

    // Create a faux HTTP response on HTTP-GET /api/user
    httpBackend.when('GET', '/user').respond(fauxUsersList);

    scope = $rootScope.$new();

    $controller('UserCtrl', {
         $scope: scope
    });
}));

afterEach(function() {
    httpBackend.verifyNoOutstandingExpectation();
    httpBackend.verifyNoOutstandingRequest();
});

it('should attach a faux users list to scope', function () {
    httpBackend.flush();

    console.log(JSON.stringif   y(scope.users));
    expect(scope.users.length).toBe(1);
});

});

0reactions
mgontocommented, Sep 30, 2013

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!

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found