mardi 5 mai 2015

How to Test a Scope Variable Initialized by a Private Controller Function?

I have a controller, simplified for brevity:

.controller('myCtrl', ['$scope', 'data', function ($scope, data) {

    $scope.myVar = testFunction(data.name);

    function testFunction (name) { ... }; //returns string or null
};

data is an object I get using the resolve property of the state in stateProvider.

I want to have two Jasmine tests that verify that $scope.myVar is:

  1. A string of some kind (doesn't really matter) when data.name has a value.
  2. null if data.name does not have a value.

My problem is that the it('test title', function () {..}); spec runs after the controller is initialized in the beforeEach function. So if I want to setup data, I need to do it in the beforeEach section. But if I do that, I determine name in advance, whereas I want it to be different for both of my tests - one time null, and one time "John" for instance.

What is the best practice when one wants to setup objects that will be used in the controller's initialization and test them with different values?

(Note: The function is private for a reason as I do not want to pollute the scope.)

Aucun commentaire:

Enregistrer un commentaire