The*_*per 6 javascript jasmine angularjs angularjs-scope angular-strap
我有一个带控制器的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();
}
});
});
Run Code Online (Sandbox Code Playgroud)
控制器规格:
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();
});
});
Run Code Online (Sandbox Code Playgroud)
我希望我的调用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.
而不是做这些.$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();
});
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7430 次 |
| 最近记录: |