我試圖單元測試(茉莉花)AngularJS和海軍報圖,但收到以下錯誤。我在應用程序的控制檯中沒有收到這些錯誤,並且圖表按預期呈現。單元測試AngularJS和FLOT圖表
PhantomJS 1.9.2 (Mac OS X) Charts Directive should populate the container element FAILED TypeError: 'undefined' is not an object (evaluating 'placeholder.css("font-size").replace') at parseOptions (/Library/WebServer/Documents/zui/app/js/libs/flot/jquery.flot.js:740) at Plot (/Library/WebServer/Documents/zui/app/js/libs/flot/jquery.flot.js:673) at /Library/WebServer/Documents/zui/app/js/libs/flot/jquery.flot.js:3059 at /Library/WebServer/Documents/zui/app/js/directives/charts.js:6 at /Library/WebServer/Documents/zui/app/js/libs/angular.js:7942 at /Library/WebServer/Documents/zui/test/unit/directives/charts.spec.js:10 at /Library/WebServer/Documents/zui/test/unit/directives/charts.spec.js:23 at invoke (/Library/WebServer/Documents/zui/app/js/libs/angular.js:2902) at workFn (/Library/WebServer/Documents/zui/app/js/libs/angular-mocks.js:1795) at /Library/WebServer/Documents/zui/app/js/libs/angular-mocks.js:1782 at /Library/WebServer/Documents/zui/test/unit/directives/charts.spec.js:24 PhantomJS 1.9.2 (Mac OS X): Executed 30 of 40 (1 FAILED) (0 secs/0.126 secs)
Charts Directive: FAILED - should populate the container element TypeError: 'undefined' is not an object (evaluating 'placeholder.css("font-size").replace') at parseOptions (/Library/WebServer/Documents/zui/app/js/libs/flot/jquery.flot.js:740) at Plot (/Library/WebServer/Documents/zui/app/js/libs/flot/jquery.flot.js:673) at /Library/WebServer/Documents/zui/app/js/libs/flot/jquery.flot.js:3059 at /Library/WebServer/Documents/zui/app/js/directives/charts.js:6 at /Library/WebServer/Documents/zui/app/js/libs/angular.js:7942 at /Library/WebServer/Documents/zui/test/unit/directives/charts.spec.js:10 at /Library/WebServer/Documents/zui/test/unit/directives/charts.spec.js:23 at invoke (/Library/WebServer/Documents/zui/app/js/libs/angular.js:2902) at workFn (/Library/WebServer/Documents/zui/app/js/libs/angular-mocks.js:1795) at /Library/WebServer/Documents/zui/app/js/libs/angular-mocks.js:1782 at /Library/WebServer/Documents/zui/test/unit/directives/charts.spec.js:24 PhantomJS 1.9.2 (Mac OS X): Executed 31 of 40 (1 FAILED) (0 secs/0.134 secs)
指令:
angular.module('directives.FlotCharts', [])
.directive('flotChart', function() {
return {
restrict: 'EA',
controller: ['$scope', '$attrs', function ($scope, $attrs) {
var plotid = '#' + $attrs.id,
model = $scope[$attrs.ngModel];
$scope.$watch('model', function (x) {
$.plot(plotid, x.data, x.options);
});
}]
};
});
控制器:
angular.module('Charts', ['directives.FlotCharts'])
.controller('diskChartCtrl', ['$scope', function ($scope) {
$scope.model = {};
$scope.model.data = [
{
label: "Available",
data: 20,
color:"#00a34a"
},
{
label: "Used",
data: 100,
color:"#c00"
}
];
$scope.model.options = {
series: {
pie: {
innerRadius: 0.5, // for donut
show: true,
label: {
formatter: function (label, series) {
return '<div class="pie">' + label + ': ' +
series.data[0][1] + 'GB <br>(' + Math.round(series.percent) + '%)</div>';
}
}
}
},
legend: {
show: false
}
};
}])
}]);
試驗規格:
describe('Charts Directive', function() {
var scope, html, tmpl, ctrl, $compile;
var compileTmpl = function (markup, scope) {
var el = $compile(markup)(scope);
scope.$digest();
return el;
};
beforeEach(function() {
module('directives.FlotCharts');
module('Charts');
inject(function ($rootScope, _$compile_, $controller) {
$compile = _$compile_;
scope = $rootScope.$new();
ctrl = $controller('diskChartCtrl', {$scope: scope});
html = angular.element('<div data-flot-chart id="disk" data-chart="pie" data-status="danger" data-ng-model="model" data-ng-controller="diskChartCtrl"></div>');
tmpl = compileTmpl(html, scope);
});
});
it('should populate the container element', function() {
expect(true).toBe(true);
//expect(tmpl.html()).toContain('canvas');
});
});
任何瞭解是極大的讚賞。
我試圖欺騙佔位符對象沒有成功。通過控制檯日誌記錄,我發現'tmpl'對象未定義。這可以通過手動編譯html字符串並通過'$ rootScope'而不是「範圍」範圍來彌補。 – Paul