简体   繁体   中英

Injecting complex Angular controller into Jasmine

I have been tasked with beginning to write tests around some legacy Angular code. And, I am a novice with Angular and beginner with Jasmine to boot.

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.

I am continue to get $injector errors but I am too confused to understand how to fix it. I am confident it has to do with the controller's dependencies, but I do not understand the syntax needed to correct it.

app.js :

'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
    });
});

Controller.js :

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);
    }
}]);

Test.js :

///<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. You generally see that in a service or factory. When given legacy code, always try to clean it up when possible.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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