Vinit Desai
Vinit Desai

Reputation: 520

Karma unit test cases not getting executed

Karma unit test cases not getting executed. There are no errors when I run the karma start my.conf.js command. There is nothing displayed in the DEBUG console. Can anyone tell what can be the issue? Here is my controller file :

define('',['angular', 'moment', '../module'], function (ng, moment) {
  'use strict';

  ng
  .module('PatientRecord.controllers')
  .controller('PatientRecordController', ["$scope", "PatientRecordService", 'globalConfig',
      function ($scope, PatientRecordService, globalConfig) {
      $scope.addSubmitted = false;
      $scope.updateSubmitted = false;
      $scope.shareSubmitted = false;
      $scope.deleteSubmitted = false;
      $scope.patientRecords = [];
      $scope.modalRecord = {};
      $scope.sharedDoctors = [];
      $scope.potentialDoctors = [];
      $scope.medicalRecordsList = false;
      $scope.disableShareButton = true;
      $scope.validation = {
                              success:{
                                status:false,
                                message:""
                              },
                              error:{
                                status:false,
                                message:""
                              }
                          };

      $scope.file = {};
      var patientRecordService = new PatientRecordService();

      $scope.getRecord = function () {
        $scope.patientRecords.length = 0;
        patientRecordService.getRecords().then(function (response) {
          $scope.patientRecords = response;
          if ($scope.patientRecords.length) {
            $scope.medicalRecordsList = true;
          }
        });
        $('body').tooltip({
            selector: '.action-icon'
        });
      };

    $scope.addPatientRecord = function ($event) {
        $scope.addSubmitted =true;
        $scope.patientRecord.shared = [];
        $scope.patientRecord.file = $scope.addRecordFileUpload;
        patientRecordService.addPatientrecorddata($scope.patientRecord)
        .then(function (response) {
            $scope.addSubmitted = false;
            clearAddRecordFields();
            $("#add-record").modal("hide");
         $scope.getRecord();

          if(response){
            $scope.validation.success.status = true;
            $scope.validation.success.message = "Record added successfully";
            $scope.addForm.$setPristine();
            $scope.addForm.addRecordFileUpload.$error.required = true;
            $scope.patientRecord.medicalRecordTypeId ="";
            $scope.addRecordFileUpload = "";
          }else{
            $scope.validation.error.status = true;
            $scope.validation.error.message = "Confirmation is unsuccessful. Please try again";
          }
        });

      };

    $scope.closeAddDialog = function () {
      clearAddRecordFields();
      $("#add-record").modal("hide");
    };

    var clearAddRecordFields = function(){
      $scope.patientRecord.name = "";
      $scope.patientRecord.dateOfRecord = "";
      $scope.patientRecord.comments = "";
      $scope.patientRecord.medicalRecordType = "";
      $scope.patientRecord.addRecordFileName = "";

      $("#patientRecord_name").val("");
      $("#dateOfRecord").val("");
      $("#patientRecord_comments").val("");
      $("#medicalRecordType").val("");
      $("#addRecordFileName").html("");
    }





    var dispalyFileName = function (FileControlId, placeholderId) {
        if ($scope.currntFileObject) {
          $('#' + placeholderId).empty().html($scope.currntFileObject.name);
        }
    };


    $scope.openupdateModal = function (record) {
      $scope.modalRecord =  _.clone(record);
        var dateOfRecord = moment($scope.modalRecord.date_of_record).format("DD MMM, YYYY");
        $scope.modalRecord.date_of_record = dateOfRecord;
        $scope.disableShareButton = true;
        $("#updateRecordFileName").html("");
        //Get Shared Doctors Data
        patientRecordService.getSharedDoctorsByRecordId(record.id).then(function (response) {
        $scope.sharedDoctors = _.where(response, { isShared: true })
        $scope.potentialDoctors = _.where(response, { isShared: false })
        });
    };

    $scope.updatePatientrecord = function (index) {
        $scope.updateSubmitted = true;
        $scope.modalRecord.medicalRecordTypeId = $("#update_recordtype").val();
        $scope.modalRecord.file = $scope.updateRecordFileUpload;
        patientRecordService.updatePatientdata($scope.modalRecord)
        .then(function (response) {
            $scope.updateSubmitted = false;
            $scope.getRecord();
         });
   };

   angular.element("#selectDoctorToShare_foreditDialog").change(function () {
        var selectedDoctorId = $("#selectDoctorToShare_foreditDialog").val();
              $scope.$apply(function () {
                  if (selectedDoctorId != 0) {
                      $scope.disableShareButton = false;
                  } else {
                      $scope.disableShareButton = true;
                  }
                });
    });




    $scope.closeUpdateDialog = function () {
        $("#patientRecord_name").val("");
        $("#datetimepicker1").val("");
        $("#patientRecord_comments").val("");
        $("#medicalRecordType").val("");
        $("#addRecordFileName").html("");
        $("#modify-record").modal("hide");
        $scope.getRecord();
    };

    $scope.openShareDialog = function (data) {
          $scope.modalRecord = data;
          $scope.disableShareButton = true;
          patientRecordService.getSharedDoctorsByRecordId(data.id)
              .then(function (response) {
                  $scope.sharedDoctors = _.where(response, { isShared: true })
                  $scope.potentialDoctors = _.where(response, { isShared: false })
                });
      }

      $scope.sharePatientRecord = function(doctorDropdownId,recordId){
          $scope.shareSubmitted  = true;
       var selectedDoctorId = $("#"+doctorDropdownId).val();
       patientRecordService.sharePatientData(recordId, selectedDoctorId).then(function(response){
           $scope.shareSubmitted  = false;
        if(response){
          if(doctorDropdownId == "selectDoctorToShare_forShareDialog") {
            $("#share-record").modal("hide");
          }
          alert("Record shared successfully");


          patientRecordService.getSharedDoctorsByRecordId($scope.modalRecord.id)
          .then(function(response) {
            $scope.sharedDoctors = _.where(response, {isShared: true})
            $scope.potentialDoctors = _.where(response, {isShared: false})
          });
         $scope.disableShareButton = true;
       } else {
        alert("Something went wrong! Try again")
      }
      });
     };

      angular.element("#selectDoctorToShare_forShareDialog").change(function () {
      var selectedDoctorId = $("#selectDoctorToShare_forShareDialog").val();
              $scope.$apply(function () {
                  if (selectedDoctorId != 0) {
                      $scope.disableShareButton = false;
                  } else {
                      $scope.disableShareButton = true;
                  }
              });
      });


     $scope.OpenDeleteModal = function (data, index) {
        $scope.modalRecord = data;
        $scope.index = index;
        $("#delete-record-modal").modal("show");
    }

    $scope.deletePatientrecord = function (data, index) {
      $scope.deleteSubmitted  = true;
        var patientRecordService = new PatientRecordService();
      patientRecordService.deletePatientdata(data.id).then(function (response) {
          $scope.deleteSubmitted  = false;
        $("#delete-record-modal").modal("hide");
        $scope.getRecord();
        if(response){
          $scope.validation.success.status = true;
          $scope.validation.success.message = "Record deleted successfully";
        }else{
          $scope.validation.error.status = true;
          $scope.validation.error.message = "Record could not be deleted";
        }

      });
    };
    $scope.getRecord();
  }
  ]);

});

Test file for controller:

// Testing PatientRecordController
    define('',['angular', 'moment', '../module'], function (ng, moment) {
    describe("Controller: PatientRecordController", function() {
        // load the controller's module
      beforeEach(module('PatientRecord.controllers'));

      var PatientRecordController,
      scope;

    // Initialize the controller and a mock scope
      beforeEach(inject(function ($controller, $rootScope) {
          scope = $rootScope.$new();
          PatientRecordController = $controller('PatientRecordController', {
                                          '$scope': scope
                                    });
      }));

      // Check if controller is defined
      it("should have a PatientRecordController as controller", function() {
        expect(PatientRecord.controllers.PatientRecordController).toBeDefined();
        console.log('controllers defined');
      });
    });
    });

my.conf.js file:

module.exports = function(config) {
  config.set({

    // base path, that will be used to resolve files and exclude
    basePath: '',


    // frameworks to use
    frameworks: ['jasmine','requirejs'],


    // list of files / patterns to load in the browser
    files: [

      'app/modules/PatientRecord/controllers/PatientRecordController.js'

    ],


    // list of files to exclude
    exclude: [

    ],


    // test results reporter to use
    // possible values: 'dots', 'progress', 'junit', 'growl', 'coverage'
    reporters: ['progress','coverage','html'],

    htmlReporter: {
        outputFile: 'tests/units.html'
    },

    preprocessors: {
      // source files, that you wanna generate coverage for
      // do not include tests or libraries
      // (these files will be instrumented by Istanbul)
      'src/**/*.js': ['coverage']
    },

    // optionally, configure the reporter
    coverageReporter: {
      type : 'html',
      dir : 'coverage/',
      file:'coverageds.txt'
    },

    // web server port
    port: 9876,


    // enable / disable colors in the output (reporters and logs)
    colors: true,


    // level of logging
    // possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG
    logLevel: config.LOG_INFO,


    // enable / disable watching file and executing tests whenever any file changes
    autoWatch: true,


    // Start these browsers, currently available:
    // - Chrome
    // - ChromeCanary
    // - Firefox
    // - Opera (has to be installed with `npm install karma-opera-launcher`)
    // - Safari (only Mac; has to be installed with `npm install karma-safari-launcher`)
    // - PhantomJS
    // - IE (only Windows; has to be installed with `npm install karma-ie-launcher`)
    browsers: ['Chrome'],


    // If browser does not capture in given timeout [ms], kill it
    captureTimeout: 60000,


    // Continuous Integration mode
    // if true, it capture browsers, run tests and exit
    singleRun: false
  });
};

Upvotes: 0

Views: 208

Answers (1)

Healforgreen
Healforgreen

Reputation: 579

The problem is in the files section of your karma configuration file. First, you need to include all your application's library dependencies (like Jquery, Angular, Bootstrap) if you are using any. Second, you need to include the files that you are testing. Include your application's initialization file (app.js) and submodules first. Last, you need to include that actual tests themselves. The order in which you include things does matter. A lot of people use RequireJS for this, but I don't think that's completely necessary unless you have a large and complex project.

Upvotes: 1

Related Questions