Karma Jasmine测试从控制器调用的$资源

时间:2015-12-01 11:18:02

标签: javascript angularjs unit-testing karma-jasmine angular-resource

我无法设法使用Karma来测试某些API调用。

这是测试文件:

describe('Requests controller test', function() {
  beforeEach(module('balrogApp.requests'));

  var ctrl, scope;
  var requestData = [
    {id: 1, project: {id: 1, title: 'Project 1'}, description: 'Some description'},
    {id: 2, project: {id: 2, title: 'Project 2'}, description: 'Another description'}
  ];

  beforeEach(inject(function($rootScope, $controller, _$httpBackend_) {
    $httpBackend = _$httpBackend_;

    $httpBackend.expectGET('/users').respond(requestData);
    $httpBackend.expectGET('/requests').respond(requestData);
    $httpBackend.expectGET('/projects').respond(requestData);
    $httpBackend.expectGET('/requestcomments').respond(requestData);
    $httpBackend.expectGET('/costestimations').respond(requestData);
    $httpBackend.expectGET('/regions').respond(requestData);

    scope = $rootScope.$new();
    ctrl = $controller('requestsController', {$scope: scope});
  }));

  afterEach(function() {
    scope.$destroy();
  });

  it('should fill properties from result from xhr requests', function() {
    var unresolvedResponse = [];

    expect(ctrl.usersList).toEqual(unresolvedResponse);
    expect(ctrl.requestsList).toEqual(unresolvedResponse);
    expect(ctrl.projectsList).toEqual(unresolvedResponse);
    expect(ctrl.requestsCommentsList).toEqual(unresolvedResponse);
    expect(ctrl.costEstimationsList).toEqual(unresolvedResponse);
    expect(ctrl.regionsList).toEqual(unresolvedResponse);

    $httpBackend.flush();

    expect(ctrl.usersList).toEqual(requestData);
    expect(ctrl.requestsList).toEqual(requestData);
    expect(ctrl.projectsList).toEqual(requestData);
    expect(ctrl.requestsCommentsList).toEqual(requestData);
    expect(ctrl.costEstimationsList).toEqual(requestData);
    expect(ctrl.regionsList).toEqual(requestData);
  });
});

我也尝试使用toBeUndefined()代替toEqual(unresolvedResponse),但它并没有改变一件事。

以下是定义$ resource的文件:

angular.module('balrogApp.services', ['balrogApp.config', 'ngResource'])
  .factory('Requests', ['$resource', 'balrogConfig', function($resource, balrogConfig) {
    return $resource(balrogConfig.backend + '/requests/:id', {id: '@id'});
  }])
  .factory('Projects', ['$resource', 'balrogConfig', function($resource, balrogConfig) {
    return $resource(balrogConfig.backend + '/projects/:id', {id: '@id'}, {'update': { method:'PUT' }});
  }])
  /*  Other factories are there */
  .factory('CostEstimations', ['$resource', 'balrogConfig', function($resource, balrogConfig) {
    return $resource(balrogConfig.backend + '/costestimations/:id', {id: '@id'});
  }]);

最后,控制器文件的一部分我正在进行测试:

angular.module('balrogApp.requests', [
  /* Dependancies */
])
  .controller('requestsController', function(Requests, Users, Projects, RequestsComments, CostEstimations,
                                             Regions, growl, $route, $rootScope, $scope, $location) {
    /* ... */

    this.usersList = Users.query();
    this.requestsList = Requests.query();
    this.projectsList = Projects.query();
    this.requestsCommentsList = RequestsComments.query();
    this.costEstimationsList = CostEstimations.query();
    this.regionsList = Regions.query();
  });

到目前为止,我收到了这个错误:

Expected [ $promise: Promise({ $$state: Object({ status: 0 }) }), $resolved: false ] to equal [ ].

我尝试将unresolvedResponse设置为此值(使用和不使用正确的语法),但它没有修复任何内容。

1 个答案:

答案 0 :(得分:1)

我自己尝试了,你如何做到这一点,正在改变你的期望

expect(ctrl.usersList).toEqual(unresolvedResponse);


 expect(ctrl.usersList.$resolved).toBeFalsy();

执行此操作将验证请求是否已发送,承诺是否已构建但尚未提供服务器响应。

我希望,这会对你有帮助。