[英]Injecting complex Angular controller into Jasmine
I have been tasked with beginning to write tests around some legacy Angular code. 我的任务是开始围绕一些旧的Angular代码编写测试。 And, I am a novice with Angular and beginner with Jasmine to boot. 而且,我是Angular的新手,还是Jasmine的初学者。
Saw one other similar post, but was answered by author adding reference paths to his file. 看到了另一篇类似的帖子,但得到作者的答复,并在其文件中添加了参考路径。 I do not believe I have that issue. 我不相信我有这个问题。
Trying to inject a controller from the Angular module so I can then write tests around its interior methods, in this case the 'addPayment' function. 尝试从Angular模块中插入一个控制器,这样我就可以围绕其内部方法(在本例中为“ addPayment”函数)编写测试。
I am continue to get $injector errors but I am too confused to understand how to fix it. 我继续遇到$ injector错误,但是我很困惑,无法理解如何解决它。 I am confident it has to do with the controller's dependencies, but I do not understand the syntax needed to correct it. 我确信它与控制器的依赖关系有关,但我不了解纠正它所需要的语法。
'use strict'
angular.module('AdminDashboard', ['ui.router', 'toastr', 'ui.bootstrap', 'checklist-model', 'ngSanitize', 'ngCsv', 'ngResource', 'angular-spinkit'])
.config(function(toastrConfig) {
angular.extend(toastrConfig, {
timeout: 5000
});
});
angular.module('AdminDashboard')
.controller('MakePaymentCtrl', ['$scope', '$state', '$stateParams', '$location', '$sce', 'paymentsFactory', '$resource', '$log', '$q', '$modal', 'toastr', function ($scope, $state, $stateParams, $location, $sce, paymentsFactory, $resource, $log, $q, $modal, toastr) {
var controller = this;
controller.loading = true;
controller.loading = true;
controller.submittedPayments = [];
controller.canPostDatePayments = function () {
return controller.CustomerModel.CanPostDatePayments.Item1;
}
controller.addPayment = function () {
var newPayment = { PaymentId: 0, Agent: controller.agent };
if (!controller.CustomerModel.CanPostDatePayments.Item1) {
newPayment.ScheduledDate = moment().format('L');
}
controller.submittedPayments.push(newPayment);
}
}]);
///<reference path="~/Scripts/jasmine/jasmine.js"/>
///<reference path="../../../../../../../../Scripts/angular.min.js"/>
///<reference path="../../../../../../../../Scripts/angular-mocks.js"/>
///<reference path="../../../../../../../../../Web.Payments/Areas/Admin/Scripts/dashboard/app.js"/>
///<reference path="../../../../../../../../../Web.Payments/Areas/Admin/Scripts/dashboard/payments/controllers/Controller.js"/>
describe('Controller: public/MakePaymentCtrl', function() {
var $controller;
beforeEach(module('AdminDashboard'));
beforeEach(inject(function(_$controller_){
$controller = _$controller_;
$controller('MakePaymentCtrl');
}));
describe("controller.addPayment", function () {
it("Contains spec with expectation", function () {
expect(true).toBe(true);
});
});
});
Yes, your dependency injections are not listed for the controller. 是的,没有为控制器列出您的依赖项注入。 You need to do something like this: 您需要执行以下操作:
beforeEach(inject(function(_$controller_, $rootScope, $state, $stateParams, $location, $sce, paymentsFactory, $resource, $log, $q, $modal, toastr){
$scope = $rootScope.$new();
$controller = _$controller_;
$controller('MakePaymentCtrl', {$scope:$scope,$state:$state,$stateParams:$stateParams,$location:$location', $sce:$sce,paymentsFactory:paymentsFactory,$resource:$resource,$log:$log,$q:$q, $modal:$modal,toastr:toastr};
}));
Also, it is generally not good practice to have $resource in a controller. 同样,在控制器中使用$ resource通常也不是一个好习惯。 You generally see that in a service or factory. 您通常会在服务或工厂看到这一点。 When given legacy code, always try to clean it up when possible. 提供旧版代码后,请尽可能尝试对其进行清理。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.