如何测试在AngularJS中调用的debug.error()函数?
我有以下AngularJS代码:如何测试在AngularJS中调用的debug.error()函数?,angularjs,unit-testing,jasmine,karma-jasmine,Angularjs,Unit Testing,Jasmine,Karma Jasmine,我有以下AngularJS代码: (function() { "use strict"; /** * @ngdoc directive * @name beckon.flint.pageHeader:pageHeader * @scope * @section pageLayout * * @restrict E * * @description * A complete page hea
(function() {
"use strict";
/**
* @ngdoc directive
* @name beckon.flint.pageHeader:pageHeader
* @scope
* @section pageLayout
*
* @restrict E
*
* @description
* A complete page header. The header transcludes additional content such as buttons into the header row.
*
* <h4>Source</h4>
* <pre>
* No controller setup needed.
* </pre>
*
* <h4>Template</h4>
* <pre>
* <page-header header="Available Dashboards" subheader="Reports">
* <flat-button ng-click="createSomething()" text="Create New Something"></flat-button>
* </page-header>
* </pre>
*
* @param {string} header - The header title.
* @param {string} subheader - The sub-header title.
* @param {string} helpPage - Link to documentation
*/
angular
.module("beckon.flint.pageHeader", ["beckon.tinder.documentation.documentationLinks"])
.directive("pageHeader", function() {
return {
templateUrl: "pageHeader",
replace: true,
transclude: true,
scope: {
header: "@",
subheader: "@",
helpPage: "@",
backState: "@?",
},
restrict: "E",
controller: function($scope, documentationLinks, svg) {
$scope.icon = svg.ARROW_LEFT;
const documentationLink = $scope.helpPage
? documentationLinks.helpPages[$scope.helpPage]
: null;
if ($scope.helpPage && !documentationLink) {
debug.error("Invalid help-page specified:" + $scope.helpPage);
}
$scope.helpUrl = documentationLink ? documentationLink.url : null;
$scope.helpText = documentationLink
? documentationLink.displayInfo.explanation
: null;
},
link: function($scope, $element, attrs, controller, transcludeFn) {
debug.log(transcludeFn);
},
};
});
})();
似乎是代码中唯一未经测试的部分,我似乎无法访问它。我尝试了spyOn(debug,“error”)
和expect(debug.error).tohavebeencall()代码>如第二个代码段所示,但该特定测试失败
有没有一种方法可以在不修改主代码的情况下测试特定的debug.error函数是否正常工作
fdescribe("pageHeader", () => {
"use strict";
let $scope;
let $compile;
let $rootScope;
let svg;
let documentationLinks;
const getIsolatedScope = (element) => element.isolateScope() || element.scope();
beforeEach(() => {
module("beckon.flint.pageHeader");
module(($provide) => {
svg = jasmine.createSpy();
documentationLinks = {
helpPages: {
MyHelpPage: {
url: "dummypage.com",
displayInfo: {
explanation: "A dummy page",
},
},
},
};
$provide.value("svg", svg);
$provide.value("documentationLinks", documentationLinks);
});
inject((_$rootScope_, _$compile_) => {
$compile = _$compile_;
$rootScope = _$rootScope_;
$scope = $rootScope.$new(true);
});
});
it("should initialize correctly", () => {
// Arrange
const element = angular.element(
"<page-header header='My header' subheader='My sub header' backState='My BackState'>Here goes the template</page-header>"
);
// Act
$compile(element)($scope);
$scope.$digest();
const isolatedScope = getIsolatedScope(element);
isolatedScope.helpPage = {};
spyOn(debug, "error");
// Assert
expect(isolatedScope.helpUrl).toBe(null);
expect(isolatedScope.helpText).toBe(null);
expect(debug.error).toHaveBeenCalled();
});
});
if ($scope.helpPage && !documentationLink) {
debug.error("Invalid help-page specified:" + $scope.helpPage);
}