[openstack-dev] [Horizon] Testing of angular promise based code paths

Richard Jones r1chardj0n3s at gmail.com
Mon Feb 29 05:42:31 UTC 2016


Hi all,

I've just added a comment to a review and I think I'd like to ask for a
broader discussion of whether I'm correct.

The review is here: https://review.openstack.org/#/c/284857/2

It boils down to: when testing code that uses a promise, should we *use* a
promise to have the follow-on callback invoked, or should we mock/spy and
then manually perform the same action the promise would if resolved?

The two forms are, broadly, pretending that a promise fired:

      it('successful submit calls the successCallback', function() {
        *var successFunc = {success: angular.noop};*
        *spyOn(successFunc, 'success');*
        spyOn(nova, 'createKeypair')*.and.returnValue(successFunc);*
        spyOn(toastService, 'add').and.returnValue({ add: angular.noop });
        ctrl.submit();
        *var successCallback = successFunc.success.calls.argsFor(0)[0];*
        *var data = {name: 'newKeypair'};*
        *successCallback(data);*
        expect(toastService.add).toHaveBeenCalledWith(
            'success',
            'Successfully imported key pair newKeypair.'
        );
      });

or actually using a promise and making it fire:

    it('should load container contents', function test() {
      *var deferred = $q.defer();*
      spyOn(swiftAPI, 'getObjects')*.and.returnValue(deferred.promise);*
      service.selectContainer('spam');
      expect(service.containerName).toEqual('spam');
      expect(swiftAPI.getObjects).toHaveBeenCalledWith('spam', {delimiter:
'/'});
      *deferred.resolve({data: {items: ['two', 'items']}});*
      *$rootScope.$apply();*
      expect(service.objects).toEqual(['two', 'items']);
      expect(service.pseudo_folder_hierarchy).toEqual([]);
    });


     Richard
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.openstack.org/pipermail/openstack-dev/attachments/20160228/b7199458/attachment.html>


More information about the OpenStack-dev mailing list