我正在尝试对(Jasmine)AngularJS 和 Flot Charts 进行单元测试,但收到以下错误。我没有在我的应用程序控制台中收到这些错误,并且图表按预期呈现。
PhantomJS 1.9.2 (Mac OS X) Charts Directive 应该填充容器元素 FAILED TypeError: 'undefined' is not an object (evalating 'placeholder.css("font-size").replace') at parseOptions (/Library/WebServer /Documents/zui/app/js/libs/flot/jquery.flot.js:740) 在 Plot (/Library/WebServer/Documents/zui/app/js/libs/flot/jquery.flot.js:673) 在/Library/WebServer/Documents/zui/app/js/libs/flot/jquery.flot.js:3059 在 /Library/WebServer/Documents/zui/app/js/directives/charts.js:6 在 /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 在调用 (/Library/WebServer/Documents/zui/app/js/libs/angular.js:2902) 在 workFn (/Library/WebServer/Documents/zui/app/js/libs/angular-mocks.js:1795 ) 在 /Library/WebServer/Documents/zui/app/js/libs/angular-mocks.js:1782 在 /Library/WebServer/Documents/zui/test/unit/directives/charts.spec.js:24 PhantomJS 1.9。 2 (Mac OS X):执行 30 of 40 (1 FAILED) (0 secs / 0.126 secs)
图表指令:失败 - 应填充容器元素 TypeError:'undefined' is not an object (evalating 'placeholder.css("font-size").replace') at parseOptions (/Library/WebServer/Documents/zui/app/ js/libs/flot/jquery.flot.js:740) 在 Plot (/Library/WebServer/Documents/zui/app/js/libs/flot/jquery.flot.js:673) 在 /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 在 /Library/WebServer/Documents/zui/test/unit/directives/charts.spec.js:10 在 /Library/WebServer/Documents/zui/test/unit/directives/charts。 spec.js:23 在调用 (/Library/WebServer/Documents/zui/app/js/libs/angular.js:2902) 在 workFn (/Library/WebServer/Documents/zui/app/js/libs/angular-mocks.js:1795) 在 /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): 执行 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');
});
});
非常感谢任何见解。