如何模拟$window单元测试AngularJS服务?
发布时间:2020-12-14 01:50:45 所属栏目:Windows 来源:网络整理
导读:这是我的服务: var services = angular.module('amdotcom.services',['ngResource']);services.factory('homePageRes',['$resource','$window',function ($resource,$window) { var wdw = angular.element($window); return $resource('home/index',{ heigh
这是我的服务:
var services = angular.module('amdotcom.services',['ngResource']); services.factory('homePageRes',['$resource','$window',function ($resource,$window) { var wdw = angular.element($window); return $resource('home/index',{ height: wdw.height(),width: wdw.width() }); }]); services.factory('homePageLoader',['homePageRes','$q',function (homePageRes,$q) { return function () { var delay = $q.defer(); homePageRes.get(function (homePage) { delay.resolve(homePage); },function () { delay.reject('Unable to fetch home page'); }); return delay.promise; }; }]); 以下是我在介绍$window服务之前的测试.这些测试工作正常,但是一旦我介绍$window服务,我就无法嘲笑它. describe('Services',function () { beforeEach(function () { module("amdotcom.services"); }); describe('homePageLoader',function () { var mockBackend,loader; beforeEach(inject(function ($httpBackend,homePageLoader) { mockBackend = $httpBackend; loader = homePageLoader; })); it('should return home page information',function () { mockBackend.expectGET('home/index?height=400&width=500').respond({ "Albums": [{ "Id": 2,"Name": "best shots" }] }); var homePageData; var promise = loader(); promise.then(function (homePg) { homePageData = homePg; }); expect(homePageData).toBeUndefined(); mockBackend.flush(); expect(homePageData.Albums[0].Id).toEqual(2); expect(homePageData.Albums[0].Name).toEqual("best shots"); }); afterEach(function () { mockBackend.verifyNoOutstandingExpectation(); mockBackend.verifyNoOutstandingRequest(); }); }); }); 我收到错误说:TypeError:’undefined’不是函数(评估’wdw.height()’) 我尝试使用$provider和spyOn方法,但没有一个工作.请帮忙. 阿伦
显然,height()和width()函数不正确.我将它们更改为innerHeight和innerWidth属性.
services.factory('homePageRes',$window) { return $resource('home/index',{ height: $window.innerHeight,width: $window.innerWidth }); }]); beforeEach(function () { angular.mock.module("amdotcom.services",function ($provide) { var myMock = { innerHeight: 400,innerWidth: 500 }; $provide.value('$window',myMock); }); }); 由于我上面的评论没有格式化和可读性,我在这里再次添加它. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- Windows Phone 7 – 如何在LongListSelector中包含ItemsPan
- Office Online Server 在线编辑Office文档,安装部署
- windows – 什么是“云操作系统”?
- Windows消息队列、线程消息队列,窗口消息的概念与关系
- windows – 如何使用每像素alpha将位图绘制到控件的画布上?
- windows – 测试用户帐户和密码的工具(测试登录)
- windows中,apache/wamp 不能正常访问thinkphp5项目
- Windows在调用Main()之前做了什么?
- windows – 如何使用VBScript从文本文件中读取?
- 局域网代码托管服务端——gitblit在windows系统中的安装(G