简体   繁体   English

使用jasmine在控制器内部自动调用测试angularjs 1工厂方法

[英]testing angularjs 1 factory method is automatically called inside a controller with jasmine

I'm using ruby on rails with angularjs one, and testing it with teaspoon-jasmine for the first time and am running into issues. 我在使用angularjs one的rails上使用ruby,并首次使用teaspoon-jasmine对其进行测试,并且遇到了问题。 Basically, I have a controller that creates an empty array and upon load calls a factory method to populate that array. 基本上,我有一个控制器,该控制器创建一个空数组,并在加载时调用工厂方法来填充该数组。 The Factory makes an http request and returns the data. 工厂发出一个http请求并返回数据。 Right now, i'm trying to test the controller, and i'm trying to test that 1) the factory method is called upon loading the controller, and 2) that the controller correctly assigns the returned data through it's callback. 现在,我正在尝试测试控制器,并且正在尝试测试1)加载控制器时调用了工厂方法,以及2)控制器通过其回调正确分配了返回的数据。 For a while I was having trouble getting a mocked factory to pass a test, but once I did, I realized I wasn't actually testing my controller anymore, but the code below passes. 一段时间以来,我一直无法通过模拟工厂来通过测试,但是一旦这样做,我意识到我实际上不再在测试控制器,但是下面的代码通过了。 Any tips on how I can still get it to pass with mock, promises/callbacks, but accurately test my controller functionality. 关于如何仍然可以通过模拟,承诺/回调进行传递,但准确测试控制器功能的任何提示。 Or should I even test the this at all in my controller since it calls a factory method and just gives it a callback? 还是我应该在我的控制器中对此进行全部测试,因为它调用了工厂方法并给了它一个回调? My 3 files are below. 我的3个文件如下。 Can anyone help here? 有人可以帮忙吗? It would be greatly appreciated 这将不胜感激

mainController.js mainController.js

 'use strict'; myApp.controller('mainController', [ 'mainFactory', '$scope', '$resource', function(factory, scope, resource){ //hits the /games server route upon page load via the factory to grab the list of video games scope.games = []; factory.populateTable(function(data){ scope.games = data; }); }]); 

mainFactory.js mainFactory.js

 'use strict'; myApp.factory('mainFactory', ['$http', '$routeParams', '$location', function(http, routeParams, location) { var factory = {}; factory.populateTable = function(callback) { http.get('/games') .then(function(response){ callback(response.data); }) }; return factory; }]); 

And finally my mainController_spec.js file 最后是我的mainController_spec.js文件

 'use strict'; describe("mainController", function() { var scope, ctrl, deferred, mainFactoryMock; var gamesArray = [ {name: 'Mario World', manufacturer: 'Nintendo'}, {name: 'Sonic', manufacturer: 'Sega'} ]; var ngInject = angular.mock.inject; var ngModule = angular.mock.module; var setupController = function() { ngInject( function($rootScope, $controller, $q) { deferred = $q.defer(); deferred.resolve(gamesArray); mainFactoryMock = { populateTable: function() {} }; spyOn(mainFactoryMock, 'populateTable').and.returnValue(deferred.promise); scope = $rootScope.$new(); ctrl = $controller('mainController', { mainFactory: mainFactoryMock, $scope: scope }); }) } beforeEach(ngModule("angularApp")); beforeEach(function(){ setupController(); }); it('should start with an empty games array and populate the array upon load via a factory method', function(){ expect(scope.games).toEqual([]) mainFactoryMock.populateTable(); expect(mainFactoryMock.populateTable).toHaveBeenCalled(); mainFactoryMock.populateTable().then(function(d) { scope.games = d; }); scope.$apply(); // resolve promise expect(scope.games).toEqual(gamesArray) }) }); 

Your code looks "non-standard" eg still using scope. 您的代码看起来是“非标准”,例如仍在使用范围。

If you are just starting with angular I hardly recommend you to read and follow this: 如果您只是从角度开始,我几乎不建议您阅读并遵循以下说明:

https://github.com/johnpapa/angular-styleguide/blob/master/a1/README.md https://github.com/johnpapa/angular-styleguide/blob/master/a1/README.md

Angular controllers cannot be tested, extract the logic into factories/services and test from there. 无法测试角度控制器,无法将逻辑提取到工厂/服务中并从那里进行测试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM