控制器中返回承诺的REST中的单元测试指南 [英] Guidance on unit testing REST call in controller returning a promise

查看:72
本文介绍了控制器中返回承诺的REST中的单元测试指南的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我能够测试-$ scope.dvModel = DienstverlenerDetailService.query();- 因此.我不知道如何测试注释掉的行.当然可以,您可以使用一些指导.

I'm able test - $scope.dvModel = DienstverlenerDetailService.query(); - accordingly. I cant figure out how to test the commented-out lines. Sure could you use some guidance on that.

angular.module('dvb.controllers').controller('ServiceFacilitatorEditController', ['$scope', 'DienstverlenerDetailService', function(

    $scope, 
    DienstverlenerDetailService) {
    'use strict';

    $scope.dvModel = DienstverlenerDetailService.query();

    /*DienstverlenerDetailService.query().$promise.then(
        function(response) {
            $scope.dvModel = response.data;
        },
        function(error) {   

        }
    );*/
}]);



describe('serviceFacilitatorEditController', function() {
    'use strict';

    beforeEach(module('dvb.ui'));

    var scope, ctrl, fact, json, $rootScope, $compile, $controller, $injector;

    beforeEach(function() {
        inject(function(_$rootScope_, _$compile_, _$controller_, _$injector_) {
            $rootScope = _$rootScope_;
            $compile = _$compile_;
            $controller = _$controller_;
            $injector = _$injector_;
        });

        jasmine.getFixtures().fixturesPath = 'base/src/main/webapp/stubs/';
        var f = readFixtures('servicefacilitator_0');
        json = JSON.parse(f);

        scope = $rootScope.$new();

        var DienstverlenerDetailService = { query: function() {} };
        spyOn(DienstverlenerDetailService, 'query').and.returnValue(json);
        ctrl = $controller('ServiceFacilitatorEditController', {
            $scope: scope,
            DienstverlenerDetailService: DienstverlenerDetailService
        });
    });

    it('it should....', function() {
        expect(scope.dvModel.data).toBe(json.data);
    });
});

看过其他一些使用$ q的帖子,$ rootscope.apply()和/或其他解决承诺的方法,但是我似乎无法将其粘在一起.我掌握了这个概念,但仍然感到奇怪的是,单行可以完美测试,而另一种表示法不是.

Looked at a couple of other posts where $q was used, $rootscope.apply() and/or other things to resolve the promise, however I cant seem to glue it together. I grasp the concept but still feel strange that that 1 single line is able to test perfectly but the other notation isnt.

我问这个的主要原因是:1-我想了解2-我不能只将诺言放在$ scope.vdModel上,我需要数据属性包装器中的内容.

The main reason for me asking this is: 1 - I want to understand 2 - I cant just put the promise on $scope.vdModel, i need whats inside the data property wrapper.

推荐答案

当您监视 DienstverlenerDetailService query 方法时.返回带有属性 $ promise 的对象,并带有值作为承诺,您可以使用 $ q.when .还要注入 $ q 服务.

When you spy on DienstverlenerDetailService's query method. Return an object with the property $promise with value as a promise, you can use $q.when. Inject $q service as well.

     spyOn(DienstverlenerDetailService, 'query').
                       and.returnValue({$promise:$q.when(json)});

并按您的期望做:-

it('it should....', function() {
    scope.$digest(); //<-- Apply digest for the promise to be resolved.
    expect(scope.dvModel.data).toBe(json.data); //<-- Now set the expectation
});

只需添加一下,当您创建模拟服务时,您可能也可以使用其他方法(我通常更喜欢),而不是创建要监视的匿名函数,您可以使用 jasmine.createSpyObj ,并在您可能要测试的模拟上添加任意数量的方法.

Just to add on, when you create a mock service you can probably do it in a different was as well (I generally prefer), instead of creating an anonymous function to be spied upon, you could create mock object using jasmine.createSpyObj and add any number of methods on the mock that you may want to test.

  DienstverlenerDetailService = jasmine.createSpyObj('DienstverlenerDetailService', ['query']);

  DienstverlenerDetailService.query.and.returnValue({$promise:$q.when(json)});

类似地,如果您还必须模拟其他方法,则可以执行以下操作:-

Similarly if you have to mock other methods as well you could do:-

 DienstverlenerDetailService = jasmine.createSpyObj('DienstverlenerDetailService', ['query', 'save', 'delete', ...etc]);

 //And set return value for specific methods...

这篇关于控制器中返回承诺的REST中的单元测试指南的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆