Unit testing BeforeEach with expectGets不能执行多次
我对指令的单元测试有一个问题,我需要先设置指令,然后才能测试它 这是我初始化它的方式:Unit testing BeforeEach with expectGets不能执行多次,unit-testing,angularjs,angularjs-directive,Unit Testing,Angularjs,Angularjs Directive,我对指令的单元测试有一个问题,我需要先设置指令,然后才能测试它 这是我初始化它的方式: var elm, $templateCache var count = 0; beforeEach(module('app/js/directives/templates/mytemplate.html')); beforeEach(inject(function($injector) { $templateCache = $injector.get('$templateCache'); }))
var elm, $templateCache
var count = 0;
beforeEach(module('app/js/directives/templates/mytemplate.html'));
beforeEach(inject(function($injector) {
$templateCache = $injector.get('$templateCache');
}));
beforeEach(inject(
['$httpBackend','$compile','$rootScope', function(_$compile) {
var template = $templateCache.get('app/js/directives/templates/mytemplate.html');
if(count == 0 ){
$httpBackend.expectGET('js/directives/templates/mytemplate.html').respond(template);
}
elm = angular.element('<div my-directive</div>');
$compile(elm)($rootScope);
if(count == 0){
$httpBackend.expectGET('/app/js/content/some.json').respond('{ "left" : "<div>Contact information</div>"}');
$httpBackend.flush();
}
$rootScope.$digest();
count++;
}]
));
afterEach(function() {
$httpBackend.verifyNoOutstandingExpectation();
$httpBackend.verifyNoOutstandingRequest();
});
it("should work like I want it to", function() {
$rootScope.my-directive.somefunction();
expect($rootScope.somevalue).toBe(2);
});
it("should load the previous page if the function scope.layover.qa_pager.prev is called", function() {
$rootScope.my-directive.someotherfunction();
expect($rootScope.somevalue).toBe(1);
});
var elm$templateCache
var计数=0;
在每个模块之前(模块('app/js/directives/templates/mytemplate.html');
每次之前(注入(函数($injector){
$templateCache=$injector.get(“$templateCache”);
}));
每次注射前(
['$httpBackend','$compile','$rootScope',函数($compile){
var template=$templateCache.get('app/js/directives/templates/mytemplate.html');
如果(计数=0){
$httpBackend.expectGET('js/directives/templates/mytemplate.html')。响应(模板);
}
elm=angular.element(“对我来说,您似乎正在尝试为单元测试加载模板。我假设您正在使用karma来运行测试,所以您应该查看html2js。它会将您的模板加载到模板缓存中
我确实试过了,但是ng-html2js是为了一个我们的团队还不能使用的新的因果报应,我确实需要弄乱路径。。
// karma.conf.js
module.exports = function(config) {
config.set({
preprocessors: {
'**/*.html': ['ng-html2js']
},
files: [
'*.js',
'*.html'
],
ngHtml2JsPreprocessor: {
// strip this from the file path
stripPrefix: 'public/',
// prepend this to the
prependPrefix: 'served/',
// or define a custom transform function
cacheIdFromPath: function(filepath) {
return cacheId;
}
}
});
};