angularjs – 如何通过jasmine避免角度测试中的location.reload(
发布时间:2020-12-17 07:08:38 所属栏目:安全 来源:网络整理
导读:在我的一个控制器中,我有一个location.reload() (对于那些想知道原因的人,我的登录页面在用户登录之前不会加载整个应用程序以降低服务器上的阈值) 当我测试控制器(使用ui的jasmine html)时,它实际上是在无限循环中重新加载测试. 我的问题是: 如何避免locati
在我的一个控制器中,我有一个location.reload()
(对于那些想知道原因的人,我的登录页面在用户登录之前不会加载整个应用程序以降低服务器上的阈值) 当我测试控制器(使用ui的jasmine html)时,它实际上是在无限循环中重新加载测试. 我的问题是: 如何避免location.reload()加载测试本身,以及如何检查它是否已被调用?我能以某种方式嘲笑它吗? 这是控制器的代码: (function() { 'use strict'; angular .module('app.ctrls') .controller('LoginController',LoginController); LoginController.$inject = [ '$scope','AuthService' ]; function LoginController($scope,AuthService) { console.log('LoginController'); $scope.credantials = { email: '',password: '',remember: false } $scope.login = function(credantials) { AuthService.login(credantials.email,credantials.password,credantials.remember || false).success(function(response) { console.log(response); if (response.email !== null) { $scope.logged = true; console.log('login successful'); location.reload(); } else { $scope.logged = false; $scope.error = true; } }).error(function() { console.log('failed to login'); }); }; } })(); 这是规格: describe("successfully logged in",function() { it("should reload the page",function() { this.scope.credantials = { email: 'test@mail.com',password: '123',remember: false }; this.$httpBackend.expectPOST('/auth/login',this.scope.credantials).respond({first_name: "Bakayaru",email: "test@mail.com"}); this.$httpBackend.whenGET('/tpls/home.html').respond(200); this.scope.login(this.scope.credantials); this.$httpBackend.flush(); expect(this.scope).toBeDefined(); expect(this.scope.logged).toBeTruthy(); /* -------> TEST that location.reload() has been called HERE <--------- */ }); }); 解决方法
我不确定你的意思是“不加载自己”,但无论如何都可以在测试中模拟.reload().我在我的应用程序中首先使用Angular服务$window.location而不是我的应用程序中的本机javascript位置,这样注入$window后作为依赖于我的控制器:
$window.location.reload(); 然后在spec文件中我有以下内容(使用模拟重新加载的一个示例测试用例): describe('Controller: MyCtrl',function () { // load the controller's module beforeEach(module('myApp')); var MyCtrl,scope,mockBackend,window; beforeEach(function(){ inject(function(_$httpBackend_,$controller,$rootScope,$window) { scope = $rootScope.$new(); mockBackend = _$httpBackend_; window = $window; spyOn(window.location,'reload'); myCtrl = $controller('MyCtrl',{ $scope: scope,$window: window }); }); }); it('logout should fail if $window.reload() is called on unsuccessful logout call',function(){ scope.logout(); mockBackend.expectGET('/rest/session/logout').respond(404); mockBackend.flush(); expect(window.location.reload.calls.count()).toEqual(0); mockBackend.verifyNoOutstandingExpectation(); mockBackend.verifyNoOutstandingRequest(); }); }); 这与您的spec文件有点不同的结构,但我希望这个例子说明如何模拟$window对象并在其上使用间谍. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |