【发布时间】:2014-10-27 18:24:14
【问题描述】:
我有一个带有控制器的 Angular 应用程序,它在函数调用期间显示 Angular-Strap 模态窗口。它在 Chrome 中正常运行,但我无法让有效的单元测试正常工作。
App 模块和 FooController:
var app = angular.module("app", ["mgcrea.ngStrap"]);
app.controller("FooController", function($scope, $modal) {
var fooModal = $modal({
title: 'Foo',
content:'Bar',
show: false,
html: true,
backdrop: 'static',
placement: 'center'});
angular.extend($scope, {
makeItFoo: function() {
fooModal.show();
}
});
});
控制器规格:
describe('FooController', function () {
var scope, controller, modal;
beforeEach(module('app', function ($provide) {
// Stub out $modal service
$provide.value('$modal', function () {
return {
hide: function () { },
show: function () { }
};
});
}));
beforeEach(inject(function ($rootScope, $controller, $injector) {
//set up a new scope and the controller for the test
scope = $rootScope.$new();
controller = $controller('FooController', {$scope: scope});
modal = $injector.get('$modal');
}));
it('should show the modal', function () {
var modalSpy = spyOn(modal(), 'show');
scope.makeItFoo();
expect(modalSpy).toHaveBeenCalled();
});
});
我希望我对 makeItFoo() 的调用会显示模态,但 Jasmine 未通过测试并出现错误 Expected spy show to have been called。我还尝试将模态的show 属性设置为true 而不是单独调用show(),并且我尝试了其他变体将$modal 服务存根并将其直接注入控制器,但它结束了出现同样的错误。
我正在使用 AngularJS 1.2.14、Angular-Strap 2.0.0 和 Jasmine 1.3.1。
【问题讨论】:
标签: javascript angularjs angularjs-scope jasmine angular-strap