Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/369.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 单元测试角度引导$modal_Javascript_Angularjs_Unit Testing_Jasmine_Angular Ui - Fatal编程技术网

Javascript 单元测试角度引导$modal

Javascript 单元测试角度引导$modal,javascript,angularjs,unit-testing,jasmine,angular-ui,Javascript,Angularjs,Unit Testing,Jasmine,Angular Ui,我在尝试为角度引导编写jasmine单元测试时遇到问题$modal。确切的错误是 预期spy open是使用[{templateUrl:'/n/views/approvement.html',controller:'w2concentmodal as w2modal',resolve:{employee:Function},size:'lg'}]调用的,但实际调用是[{templateUrl:'/n/views/approvement.html',controller:'w2concentmod

我在尝试为角度引导编写jasmine单元测试时遇到问题
$modal
。确切的错误是
预期spy open是使用[{templateUrl:'/n/views/approvement.html',controller:'w2concentmodal as w2modal',resolve:{employee:Function},size:'lg'}]调用的,但实际调用是[{templateUrl:'/n/views/approvement.html',controller:'w2concentmodal as w2modal',resolve:{employee:Function},size:'lg}]

预期模态选项对象与实际模态选项对象相同。发生了什么事

控制器 试验 这是一个问题。
$modal.open中使用的
resolve.employee
匿名函数:

var modalInstance = $modal.open({
    templateUrl: '/n/views/consent.html',
    controller: 'W2ConsentModal as w2modal',
    resolve: {
        employee: function () {
            return employee;
        }
    },
    size: 'lg'
});
与测试中的
resolve.employee
匿名函数不相同(通过引用):

var modalOptions = {
    templateUrl: '/n/views/consent.html',
    controller: 'W2ConsentModal as w2modal',
    resolve: {
        employee: function () {
            return employee;
        }
    },
    size: 'lg'
};
你的测试应该是:

resolve: {
    employee: jasmine.any(Function)
}
如果必须测试resolve函数,则应将其公开在某个地方,以便在测试中获得对同一函数的引用。

尝试以下方法:

describe('W2History controller', function () {
        var controller, scope, modal;

        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 modalOptions = {
            templateUrl: '/n/views/consent.html',
            controller: 'W2ConsentModal as w2modal',
            resolve: {
                employee: jasmine.any(Function)
            },
            size: 'lg'
        };

        var actualOptions;

        beforeEach(function () {
            module('plunker');

            inject(function (_$controller_, _$rootScope_, _$modal_) {
                scope = _$rootScope_.$new();                         
                modal = _$modal_;

                spyOn(modal, 'open').and.callFake(function(options){
                    actualOptions = options;

                    return fakeModal;
                });

                controller = _$controller_('W2History', {
                    $scope: scope,
                    $modal: modal
                });

            });

        });

        it('Should correctly show the W2 consent modal', function () {
            var employee = { name : "test"};

            controller.showModal(employee);
            expect(modal.open).toHaveBeenCalledWith(modalOptions);
            expect(actualOptions.resolve.employee()).toEqual(employee);
        });
    });

说明

我们不应该期望实际的
resolve.employee
与假
resolve.employee
相同,因为
resolve.employee
是一个返回员工的函数(在这种情况下,员工在闭包中被捕获)。函数可以相同,但运行时返回的对象可能不同

测试失败的原因是javascript比较函数的方式。看看这个。无论如何,我不关心这个,因为我们不应该期望函数实现。在本例中,我们真正关心的是
解析。employee
在传入时返回相同的对象:

expect(actualOptions.resolve.employee()).toEqual(employee);
因此,这里的解决方案是: 除了解决员工问题之外,我们希望得到一切:

var modalOptions = {
                templateUrl: '/n/views/consent.html',
                controller: 'W2ConsentModal as w2modal',
                resolve: {
                    employee: jasmine.any(Function) //don't care about the function as we check it separately.
                },
                size: 'lg'
            };

   expect(modal.open).toHaveBeenCalledWith(modalOptions);
通过首先捕获,分别检查
resolve.employee

var actualOptions;

 spyOn(modal, 'open').and.callFake(function(options){
      actualOptions = options; //capture the actual options               
      return fakeModal;
 });

expect(actualOptions.resolve.employee()).toEqual(employee); //Check the returned employee is actually the one we pass in.

我不确定这是否对您有帮助,但是当您监视某个东西时,可以获取传递给$uibModal.open spy的参数,然后可以调用该函数来测试它是否返回resolve方法中的内容

it('expect resolve to be have metadataid that will return 9999', () => {
            spyOn($uibModal, 'open');
            //add test code here that will call the $uibModal.open
            var spy = <jasmine.Spy>$uibModal.open;
            var args = spy.calls.argsFor(0);
            expect(args[0].resolve.metadataId()).toEqual(9999);
});
it('expect-resolve-to-have-metadataid将返回9999',()=>{
spyOn($uibModal,'open');
//在此处添加将调用$uibModal.open的测试代码
var spy=$uibModal.open;
var args=spy.calls.argsFor(0);
expect(args[0].resolve.metadataId()).toEqual(9999);
});

*****我的代码使用的是typescript,但这对我来说很有效。**

我遇到过同样的情况。我在下面给出的解决方案中遇到了这个问题

//Function to open export modal
scope.openExportModal();
expect( uibModal.open ).toHaveBeenCalledWith(options);
expect( uibModal.open.calls.mostRecent().args[0].resolve.modalData() ).toEqual(modalData);

希望这能帮助您快速解决问题。

谢谢您的回答,但是将我的测试改为使用
员工:jasmine。任何(函数)
或只是
员工:函数
仍然无法通过测试您使用缩小保护技术的解析函数?我的是防止茉莉花。任何(功能)的工作如此简单!非常感谢。这困扰了我一段时间。
it('expect resolve to be have metadataid that will return 9999', () => {
            spyOn($uibModal, 'open');
            //add test code here that will call the $uibModal.open
            var spy = <jasmine.Spy>$uibModal.open;
            var args = spy.calls.argsFor(0);
            expect(args[0].resolve.metadataId()).toEqual(9999);
});
//Function to open export modal
scope.openExportModal();
expect( uibModal.open ).toHaveBeenCalledWith(options);
expect( uibModal.open.calls.mostRecent().args[0].resolve.modalData() ).toEqual(modalData);