Injecting complex Angular controller into Jasmine

时间:2016-02-12 21:30:33

标签: javascript angularjs 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 :

ORI

Controller.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
    });
});

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

1 个答案:

答案 0 :(得分:0)

Yes, your dependency injections are not listed for the controller. You need to do something like this:

@AfterMethod
public void printLOGonFailure(ITestResult result) {
    if (result.getStatus() == ITestResult.FAILURE) {
        String  str = getLog();
        Reporter.log(str);
    }
}

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.