Karma意外请求:GET modules / home / home.html

时间:2015-09-23 21:59:06

标签: angularjs unit-testing karma-runner

我刚刚使用Jade模板引擎切换我的应用程序以使用客户端HTML,以提高性能并减少服务器请求。在应用程序中一切正常,但是我在更新单元测试时遇到了问题。

我有以下测试:

describe('Registration Controller Tests', function() {
  var $controller, $scope, defer, registerSpy, doesUserExistSpy, auth, RegistrationCtrl;

  beforeEach(module('enigmaApp'));

  beforeEach(inject(function (_$controller_, _$rootScope_, $q) {
    $controller = _$controller_;
    $scope = _$rootScope_;
    defer = $q.defer();

    // Create spies
    registerSpy = jasmine.createSpy('register').and.returnValue(defer.promise);
    doesUserExistSpy = jasmine.createSpy('doesUserExist').and.returnValue(defer.promise);

    auth = {
      register: registerSpy,
      doesUserExist: doesUserExistSpy
    };    

    // Init register controller with mocked services and scope
    RegistrationCtrl = $controller('RegistrationCtrl', {
      $scope: $scope,
      auth: auth
    });

    // digest to update controller with services and scope
    $scope.$digest();
  }));

  describe('RegistrationCtrl.register()', function () {
    beforeEach(function () {
      $scope.user = {
        email: 'bwayne@wayneenterprise.com',
        first_name: 'Bruce',
        last_name: 'Wyane',
        password: 'password123'
      }
    });

    it('should call auth.register() with $scope.user', function () {
      $scope.register();
      expect(auth.register).toHaveBeenCalledWith($scope.user);
    });
});

导致以下错误:

Error: Unexpected request: GET modules/home/home.html
No more requests expected

为了模仿路线,我需要做些什么?到目前为止,我已经尝试了一些事情,但没有尝试过。

附加代码:

RegistrationCtrl

 .controller('RegistrationCtrl', function($scope, $state, auth) {
     $scope.user = {};
     $scope.userExists = false;
     $scope.error = '';

     $scope.register = function() {
       auth.register($scope.user)
       .then(function(response){
         $state.go('secure.user'); 
       })
       .catch(function(err){
         $scope.error = err;
       });
     };
 });

1 个答案:

答案 0 :(得分:0)

假设您的静态文件都在/ modules中:

$httpBackend.whenGET(/modules\/[\w\W]*/).passThrough();