How to inject a Factory when creating a unit test with Angularjs?

Asked

Viewed 58 times

1

good afternoon! I am creating a unit test for my controller and the same is giving the following error:

Typeerror: Cannot read Property 'saveUser' of Undefined
At Scope.UserFormCtrl.$Scope.save

I saw that the problem should be because of the dependencies that my controller has. Follow my files for better understanding.

Controller

let UserFormCtrl = ($scope, UserFactory, UserListService) => {
  $scope.user = {};
  $scope.lista = [{
      name: 'ericke',
      idade: 18,
      password: 123
    },
    {
      name: 'teste',
      idade: 18,
      password: 1234
    }
  ];
  $scope.save = () => {
    UserFactory.saveUser($scope.user, (err, user) => {
      //TODO: handle error

      $scope.lista = UserListService.get();
      $scope.lista.push(user.data);
    });

    $scope.reset();
  }

  $scope.reset = () => {
    $scope.user = {};
    $scope.photoName = '';
  }

  $scope.validaPhoto = () => {
    let array = ['jpeg', 'png', 'jpg'];
    let extension = this.user.photo.name.split(".");

    if (array.indexOf(extension[extension.length - 1]) == -1) {
      Materialize.toast('Invalid file extension!', 3500);
      $scope.photoName = '';
    }
  }

}

export default UserFormCtrl;

Factory:

let UserFactory = function($http) {

    const API_BASE_PATH = 'http://localhost:5000/api/manager/users';

    return {

      saveUser: (user, callback) => {
        $http({
          method: 'POST',
          url: API_BASE_PATH,
          data: user
        }).then(function success(data) {
          callback(null, data.data);
        }, function error(err) {
          callback(err);
        });
      }


    };

};

export default UserFactory;

Test file:

import UserFormModule from './userForm'
import UserFormController from './userForm.controller';
import UserFormTemplate from './userForm.html';

describe('UserForm', () => {

  let scope, UserFactory, UserListService, controller;

  beforeEach(window.module(UserFormModule));
  beforeEach(inject(function($controller, $rootScope) {
    scope = $rootScope; // this is what you missed out
    controller = $controller(UserFormController, {
      $scope: scope,
      UserFactory: UserFactory,
      UserListService: UserListService,
    });
  }));

  describe('Controller', () => {

    it('Reset user', () => {
      scope.user = {
        email: '[email protected]',
        password: 123
      };
      scope.reset();
      expect(scope.user.email).to.equal(undefined);
      expect(scope.user.password).to.equal(undefined);
    });

    it('Save User', () => {
      let listLenght = scope.lista.length;
      console.log('Tamanho lista: ', listLenght);
      scope.user = {
        email: '[email protected]',
        password: 123
      };
      scope.save();

      expect(scope.lista.length).to.equal(listLenght + 1);
    });
  });
});
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.