lundi 23 mai 2016

karma test, relead controller with different data set

I have a controller that I am writing tests for and the controller has a function that is run when the controller is loaded. This function has an if statement that I want to test for both scenarios. My problem is that the $scope I'm loading is only set for the "if" and not the else so I am not sure how to test the else. I tried to set "skillcategoriesList" to an array that would make the function go into the else portion but that doesn't work. Is there a way to reload the controller when testing the "it"?

This is the function in my controller

  function loadSkills() {
    if (!SkillsService.skillcategoriesList.length) {
      SkillsService.getSkillCategories().then(function(res) {
        SkillsService.skillsLoaded = true;
       });
    } else {
       SkillsService.skillcategories = SkillsService.parseSubCategories(SkillsService.skillcategoriesList);
      SkillsService.skillsLoaded = true;
    }
  }

  loadSkills();

And in my test I have

beforeEach(function() {
  return inject(function($injector) {
      this.SkillsService = {
      skillcategoriesList: [],
      getSkillCategories: function() {
        var deferred = $q.defer();
        deferred.resolve();
        return deferred.promise;
      },
      parseSubCategories: function(param) {
        return param;
      }
  };

So the test loads up skillcategoriesList as an empty array which i have a test for that portion but I want to test the else where I need skillcategoriesList to be [1, 2]. Ideas?

Aucun commentaire:

Enregistrer un commentaire