在Jasmine中未定义的$ promise.then

时间:2016-08-11 16:13:22

标签: angularjs karma-jasmine

我在控制器中的函数中有一个服务查询,它返回一个promise。但是,我的规范继续返回以下错误:

  

TypeError:'undefined'不是对象(评估   'staffUsers.query()。$ promise.then')

我的规格:

'use strict';

describe('Controller: ManagePermissionsController', function () {
  beforeEach(module('MessagingAppApp'));
  var scope = null,
    ctrl = null,
    httpBackend = null,
    $q,
    deferred,
    mockService,
    mockServiceFacility,
    staffUserMock,
    $controller,
    baseUrl,
    regex;
  mockService = {
    update: function () {
      return 'ok';
    }
  };
  mockServiceFacility = {
    get: function(){
      return [{_id: 'dfn-777', name: 'Test Facility'}];
    }
  };
  staffUserMock = {
    query: function () {
      return [{
        "userIdentifier": {
          "uniqueId": "1",
          "assigningAuthority": "dfn-777"
        },
        "roles": ["admin", "bcm_admin"],
        "firstName": "Test",
        "lastName": "User",
        "middleName": null
      }];
    }
  };
  beforeEach(inject(function ($injector, _$httpBackend_, $rootScope, $controller) {
    scope = $injector.get('$rootScope');
    $controller = $injector.get('$controller');
    httpBackend = _$httpBackend_;

    ctrl = $controller('ManagePermissionsController', {
      $scope: scope,
      staffManagement: mockService,
      facilityService: mockServiceFacility,
      staffUsers: staffUserMock
    });


    httpBackend.when('GET', 'manage/managePermissions.tpl.html').respond(204);
    httpBackend.when('GET', 'scripts/modules/connection/connection_error.tmpl.html').respond(204);
    httpBackend.when('GET', 'views/patientSearch.tpl.html').respond(204);
    httpBackend.when('GET', 'scripts/modules/connection/connection_server.tmpl.html').respond(204);
    httpBackend.when('GET', new RegExp('service/staff/userList*', '')).respond(204);
    httpBackend.when('GET', new RegExp('service/resource-directory/tokenurls*', '')).respond(204);
  }));
  it('update a users roles', function() {
    scope.searchLast = 'User';
    scope.searchType = 'staff';
    spyOn(staffUserMock, 'query').andCallThrough();
    scope.$root.$digest();
    expect(staffUserMock.query).toHaveBeenCalled();
    scope.triggerSearch();
    expect(scope.adminList[0].lastName).toBe('User');
    scope.selectStaffUser(scope.adminList[0]);
    expect(scope.userSelected).toBe(true);
    expect(scope.selectedUser.lastName).toBe('User');
    scope.selectedUser.broadcast = false;
    scope.updateUserRoles(scope.selectedUser);
    expect(scope.data.roles.indexOf('bcm_admin')).toBe(-1);
  });
});

0 个答案:

没有答案