[英]Unit testing $modal with Jasmine
I have an Angular app with a controller which displays an Angular-Strap
modal window during a function call. 我有一个带有控制器的Angular应用程序,它在函数调用期间显示Angular-Strap
模态窗口。 It functions correctly in Chrome, but I am at a loss getting a valid unit test working. 它在Chrome中正常运行,但我无法进行有效的单元测试。
App module and the FooController: 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();
}
});
});
Controller spec: 控制器规格:
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();
});
});
Here's a fiddle as well. 这也是一个小提琴。
I expect my call to makeItFoo()
to display the modal, but Jasmine fails the test with the error Expected spy show to have been called
. 我希望我调用makeItFoo()
来显示模态,但Jasmine未通过测试,错误Expected spy show to have been called
。 I've also tried setting the show
property of the modal to true
and not calling show()
separately, and I've tried other variants of stubbing the $modal service and injecting it directly into the controller, but it ends up with the same error. 我也尝试将模态的show
属性设置为true
而不是单独调用show()
,并且我尝试了其他变体来存储$ modal服务并将其直接注入控制器,但它最终都是相同的错误。
I'm using AngularJS 1.2.14, Angular-Strap 2.0.0, and Jasmine 1.3.1. 我正在使用AngularJS 1.2.14,Angular-Strap 2.0.0和Jasmine 1.3.1。
Instead of doing these. 而不是做这些。 Create a mock object for $modal
with show and hide methods and set your expectations on them. 使用show和hide方法为$modal
创建一个模拟对象,并设置对它们的期望。
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();
});
});
The modal show is async. 模态显示是异步的。 I updated your fiddle at http://jsfiddle.net/jwom7ns2/1/ . 我在http://jsfiddle.net/jwom7ns2/1/更新了你的小提琴。
Change the following portion: 更改以下部分:
it('should show the modal', function (done) {
var modalSpy = spyOn(modal(), 'show');
scope.makeItFoo();
setTimeout(function() {
expect(modalSpy).toHaveBeenCalled();
done();
});
});
The timeout wrapper waits for the digest to happen when the modal show occurs. 当模态显示发生时,超时包装器等待摘要发生。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.