Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angularjs 使用服务的单元测试指令_Angularjs_Unit Testing_Angularjs Directive_Dependency Injection_Karma Jasmine - Fatal编程技术网

Angularjs 使用服务的单元测试指令

Angularjs 使用服务的单元测试指令,angularjs,unit-testing,angularjs-directive,dependency-injection,karma-jasmine,Angularjs,Unit Testing,Angularjs Directive,Dependency Injection,Karma Jasmine,我有一个使用服务的指令: angular.module('app', []) .service('myService', function() { return { getCustomer: function(){ return { name: 'Naomi', address: '1600 Amphitheatre' }; } }; }) .directive('myCustomer', function(mySe

我有一个使用服务的指令:

angular.module('app', [])
.service('myService', function() {
  return {
    getCustomer: function(){
      return {
        name: 'Naomi',
        address: '1600 Amphitheatre'
      };
    }
  };
})
.directive('myCustomer', function(myService) {
  return {
    link: function(scope){
      scope.customer = myService.getCustomer();
    },

    template: 'Name: {{customer.name}} Address: {{customer.address}}'
  };
});
我试图对此进行单元测试,但我似乎不知道如何在单元测试中将服务注入到我的指令中

var tests;
(function (tests) {
    describe('myCustomer Directive', function () {
        var scope, createDirective;

        beforeEach(angular.mock.module('app'));
        beforeEach(angular.mock.module('templates'));

        beforeEach(angular.mock.inject(function ($injector) {
            var $compile = $injector.get('$compile');

            var myService = $injector.get('myService');

            scope = $injector.get('$rootScope');

            // where do I inject myService???

            createDirective = function () {
                return $compile('<my-customer></my-customer>')(scope);
            };
        }));
        describe('on creation', function () {
            var sut;

            beforeEach(function () {
                sut = createDirective();
                scope.$digest();
            });

            it('creates an element node', function () {
                var contents = sut.contents();
                expect(contents[0].nodeType).toBe(sut[0].ELEMENT_NODE);
            });
        });
    });
})(tests || (tests = {}));
var检验;
(功能测试){
描述('myCustomer指令',功能(){
var范围,createDirective;
每个之前(angular.mock.module('app'));
beforeach(angular.mock.module('templates');
beforeach(angular.mock.inject)(函数($injector){
var$compile=$injector.get(“$compile”);
var myService=$injector.get('myService');
scope=$injector.get(“$rootScope”);
//我在哪里注入我的服务???
createDirective=函数(){
返回$compile(“”)(范围);
};
}));
描述('创建时',函数(){
萨特变种;
beforeach(函数(){
sut=createDirective();
范围。$digest();
});
它('创建元素节点',函数(){
var contents=sut.contents();
expect(contents[0].nodeType).toBe(sut[0].ELEMENT\u NODE);
});
});
});
})(测试| |(测试={}));
问题是,我需要能够显式地注入依赖项,以便模拟它的一些调用。这可能吗


使用我的应用程序代码。

正如JB Nizet所指出的那样,你所需要做的就是监视该服务,它会自行处理

    beforeEach(angular.mock.inject(function ($injector) {
        var $compile = $injector.get('$compile');
        scope = $injector.get('$rootScope');

        var myService = $injector.get('myService');

        // set up spies
        spyOn(myService, "getCustomer").and.returnValue({foo: 'bar'});

        createDirective = function () {
            return $compile('<my-customer></my-customer>')(scope);
        };
    }));
beforeach(angular.mock.inject)(函数($injector){
var$compile=$injector.get(“$compile”);
scope=$injector.get(“$rootScope”);
var myService=$injector.get('myService');
//设置间谍
spyOn(myService,“getCustomer”).and.returnValue({foo:'bar'});
createDirective=函数(){
返回$compile(“”)(范围);
};
}));

您不需要注入它。Angular为你注射,这就是问题所在:它不是。我收到一个未知的提供程序错误。如果您有错误,请在问题中发布完整的错误消息/stacktrace。事实证明,未知的提供程序错误是不相关的,但我仍然需要能够显式注入依赖项,以便我可以模拟它。不要模拟它。监视它。最终结果是一样的,但更容易。