0

I tried test a function enside a controller in angularjs, but always fail.

this is for angularjs project

//controller---

'use strict';

 angular.module('My_module').controller('my_controller', function (
 $scope, $route, $timeout, $routeParams) {

   function first_function(data) {
     return and_array;
   }

   function second_function() {
     return and_obj;
   }
 }

//-----Spec----

'use strict';

 describe('My tests specs', function () {
  var route;
  var scope;

  var my_controller;
  beforeEach(function () {
    controller();
  });

  describe('first test', function(){
  it('test functions', fucntion(){
   expect(my_controller_Mock.first_function).toHaveBeenCalled();
  });
  });

  function controller() {inject(function ($rootScope, $controller, 
     $route) {
     route = $route;
     scope = $rootScope.$new();

  //--i tried these
   my_controller_Mock = jasmine.createSpyObj('my_controller', 
   ['first_function'])

  $controller('my_controller', {
    $scope: scope,
    $route: route,
    my_controller: my_controller_Mock
  });
 }

jasmine Error: Expected spy my_controller.first_function to have been called

2
  • 1
    You are never calling your function controller(). Commented Jul 26, 2019 at 19:47
  • Sorry now add de call to controller. same error Commented Jul 26, 2019 at 19:50

1 Answer 1

1

In your example, you're never calling first_function, so jasmine is correct that's it's not being called.

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

Comments

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.