0

I am new to writing unit test. I am trying simple thing, In my saveInfo function in controller $scope.practice should contain a value.

I am using webstorm to run my test. I want to check in saveInfo function that $scope.practice is defined or not.

test / practice.js

'use strict';
describe('myApp', function() {
    describe('Controller: PracticeCtrl', function () {
        var $http, $scope, $routeParams, $location, $locale, $timeout;
      // load the controller's module
        beforeEach(function () {
            // Load the controller's module
            module('myApp');

            inject(function ($controller, $rootScope) {
                $scope = {};
            });

        });

        it('should define a practice property', function () {
            expect($scope.practice).toBeDefined();
        });

    });
});

practice.js - controller

angular.module('myApp').controller('PracticeCtrl', function ($http, $scope, $routeParams, $location, $locale, $timeout) {

$scope.saveInfo = function () {
        $scope.practice = '52300099';
        Practices.updateStampApproval().updateInfo($scope.practice);  

 };

$scope.updateInfo = function () {
   ....
}

$scope.getInfo = function () {
   ....
}

Gives me Error :-

Expected undefined to be defined.
Error: Expected undefined to be defined.

In my controller $scope.practice is defined. so why it shows error.

And how to test function wise means how do i write test which will check only in 1 function in controller e.g. 'saveInfo'?

1 Answer 1

2

You're initializing $scope as an empty object, and it's not being manipulated anywhere. That's why your test is failing.

What you should do:

  1. Initialize your controller with a dummy $scope
  2. Invoke each of your controller methods in each test
  3. Do your assertions in each test

For example:

describe('myApp', function() {
    describe('Controller: PracticeCtrl', function () {
        var $http, $scope, $routeParams, $location, $locale, $timeout, practiceCtrl;

        // load the controller's module
        beforeEach(function () {
            // Load the controller's module
            module('myApp');

            inject(function ($controller, $rootScope) {
                $scope = $rootScope.$new();
                practiceCtrl = $controller("PracticeCtrl", {
                    $scope: $scope
                });
            });

        });

        it('should define a practice property', function () {
            $scope.saveInfo();
            expect($scope.practice).toBeDefined();
        });

    });
});

I hope I was clear enough to help you.

Sign up to request clarification or add additional context in comments.

5 Comments

this became clear but i have one doubt...if i use a service in my controller which brings a list of practices, Will i be able to call this service in my test.js?
Yes. You can mock/stub it to return a few example practices, so you don't have to rely on the server's data.
i actually want the server's data...coz then i will not have to write a mock service & instead use the real service...
you can share if you know any tutorial where i can learn this step by step.....i searched google, but haven't got perfect one.
I don't exactly have any tutorial right now. And I said to not rely on the server because it could break your tests if you reinstall your app (so your DB would have no data)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.