我有一个带有控制器的Angular应用,该控制器Angular-Strap
在函数调用期间显示模式窗口。它在Chrome中可以正常运行,但是让有效的单元测试正常工作我很茫然。
应用模块和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失败,并显示error Expected spy show to have been called
。我还尝试将show
modal的属性设置为,true
而不是show()
单独调用,并且尝试了其他方式,将$ modal服务存根并将其直接注入到控制器中,但最终会出现相同的错误。
我正在使用AngularJS 1.2.14,Angular-Strap 2.0.0和Jasmine 1.3.1。
而不是做这些。$modal
使用show和hide方法创建一个模拟对象,并对它们设置期望。
describe('FooController', function () {
var scope, controller, modal;
beforeEach(module('app'));
beforeEach(inject(function ($rootScope, $controller) {
//set up a new scope and the controller for the test
scope = $rootScope.$new();
//Create spy object
modal = jasmine.createSpyObj('modal', ['show', 'hide']);
//provide modal as dependency to the controller.
controller = $controller('FooController', {$scope: scope, $modal:modal});
}));
it('should show the modal', function () {
scope.makeItFoo();
expect(modal.show).toHaveBeenCalled();
});
});
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句