Javascript 用Jasmine模仿Angular中的模态实例
我试图测试角度模式的打开,但遇到以下错误:Javascript 用Jasmine模仿Angular中的模态实例,javascript,angularjs,twitter-bootstrap,unit-testing,jasmine,Javascript,Angularjs,Twitter Bootstrap,Unit Testing,Jasmine,我试图测试角度模式的打开,但遇到以下错误: Error: Unexpected request: GET app/templates/editComment/editComment.html No more request expected 以下是我正在测试的代码: vm.editComment = function (comment) { vm.modalInstance = $modal.open({ templateUrl: 'app/templa
Error: Unexpected request: GET app/templates/editComment/editComment.html
No more request expected
以下是我正在测试的代码:
vm.editComment = function (comment) {
vm.modalInstance = $modal.open({
templateUrl: 'app/templates/editComment/editComment.html',
controller: 'EditCommentCtrl as vm',
comment: comment,
resolve: {
comment: function () {
return comment;
}
}
}).result.then(function (result) {
vm.getComments();
});
}
测试设置:
beforeEach(inject(function ($rootScope, $controller, $q, $sce) {
q = $q;
var stateParam = {id: 1};
scope = $rootScope.$new();
var Listmanager = "";
var fakeModal = {
result: {
then: function(confirmCallback, cancelCallback) {
//Store the callbacks for later when the user clicks on the OK or Cancel button of the dialog
this.confirmCallBack = confirmCallback;
this.cancelCallback = cancelCallback;
}
},
close: function( item ) {
//The user clicked OK on the modal dialog, call the stored confirm callback with the selected item
this.result.confirmCallBack( item );
},
dismiss: function( type ) {
//The user clicked cancel on the modal dialog, call the stored cancel callback
this.result.cancelCallback( type );
}};
var modalInstance = {
open: jasmine.createSpy('modalInstance.open')
}
modalInstance.open.and.returnValue(fakeModal);
ctrl = $controller('CommentsCtrl', { $scope: scope, $modalInstance: modalInstance, ds: dsMock, $stateParams: stateParam, $sce: $sce, Listmanager: Listmanager, ns: nsMock });
}));
这是我的测试:
it('edit comments should open modal', inject(function () {
var comment = "test";
ctrl.editComment(comment);
scope.$apply();
expect(modalInstance.open).toHaveBeenCalled();
}));
我已经研究了这两个问题,并试图得到一些答案,但迄今为止,我所尝试的都没有奏效
任何帮助都将不胜感激。Modal.open需要回复承诺,但也需要成为间谍,以防你不想看到结果 如果我们这样做的话:
open: jasmine.createSpy('modal.open')
这在大多数情况下都有效,但我们希望得到一个承诺,因此如果我们这样做:
beforeEach(inject(function ($rootScope, $controller, $q, $sce) {
q = $q;
var stateParam = {id: 1};
scope = $rootScope.$new();
var Listmanager = "";
modal = {
open: jasmine.createSpy('modal.open').and.returnValue({ result: { then: jasmine.createSpy('modal.result.then') } }),
close: jasmine.createSpy('modal.close'),
dismiss: jasmine.createSpy('modal.dismiss')
};
ctrl = $controller('CommentsCtrl', { $scope: scope, $modal:modal, ds: dsMock, $stateParams: stateParam, $sce: $sce, Listmanager: Listmanager, ns: nsMock });
}));
它应该像一个魅力 Modal.open需要返回一个承诺,但也需要是一个间谍,以防你不想要结果 如果我们这样做的话:
open: jasmine.createSpy('modal.open')
这在大多数情况下都有效,但我们希望得到一个承诺,因此如果我们这样做:
beforeEach(inject(function ($rootScope, $controller, $q, $sce) {
q = $q;
var stateParam = {id: 1};
scope = $rootScope.$new();
var Listmanager = "";
modal = {
open: jasmine.createSpy('modal.open').and.returnValue({ result: { then: jasmine.createSpy('modal.result.then') } }),
close: jasmine.createSpy('modal.close'),
dismiss: jasmine.createSpy('modal.dismiss')
};
ctrl = $controller('CommentsCtrl', { $scope: scope, $modal:modal, ds: dsMock, $stateParams: stateParam, $sce: $sce, Listmanager: Listmanager, ns: nsMock });
}));
它应该像一个魅力