栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

单元测试angular-bootstrap $ modal

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

单元测试angular-bootstrap $ modal

试试这个:

describe('W2History controller', function () {        var controller, scope, modal;        var fakeModal = { result: {     then: function (/confirm/iCallback, cancelCallback) {         //Store the callbacks for later when the user clicks on the OK or Cancel button of the dialog         this.confirmCallBack = /confirm/iCallback;         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比较函数的方式。看看这个小提琴。无论如何,我对此并不在乎,因为我们不应该期望
函数实现 。在这种情况下,我们关心的是

resolve.employee
返回与传入的对象相同的对象:

expect(actualOptions.resolve.employee()).toEqual(employee);

因此,这里的解决方案是:我们期望除之外的所有东西

resolve.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.


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/436351.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号