在角JS,而测试控制器得到了未知供应商 [英] in angular js while testing the controller got Unknown provider

查看:85
本文介绍了在角JS,而测试控制器得到了未知供应商的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下的控制器

angular.module('samples.controllers',[])
  .controller('MainCtrl', ['$scope', 'Samples', function($scope, Samples){
  //Controller code
}

这取决于以下的服务

angular.module('samples.services', []).
    factory('Samples', function($http){
    // Service code
}

试过的测试控制器使用以下code:

describe('Main Controller', function() {
  var service, controller, $httpBackend;

  beforeEach(module('samples.controllers'));
  beforeEach(module('samples.services'));
  beforeEach(inject(function(MainCtrl, Samples, _$httpBackend_) {

  }));

    it('Should fight evil', function() {

    });
});

但是得到了以下错误:

Error: Unknown provider: MainCtrlProvider <- MainCtrl.

P.S尝试下面的帖子,似乎并没有帮助

推荐答案

要测试控制器的正确方法是使用$控制器这样的:

The correct way to test controllers is to use $controller as such:

CTRL = $控制器('MainCtrl',{$适用范围:适用范围,样品:服务});

详细例如:

describe('Main Controller', function() {
  var ctrl, scope, service;

  beforeEach(module('samples'));

  beforeEach(inject(function($controller, $rootScope, Samples) {
    scope = $rootScope.$new();
    service = Samples;

    //Create the controller with the new scope
    ctrl = $controller('MainCtrl', {
      $scope: scope,
      Samples: service
    });
  }));

  it('Should call get samples on initialization', function() {

  });
});

这篇关于在角JS,而测试控制器得到了未知供应商的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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