Stack Exchange
Angular mock dependency function
I've got the following angular module
var app = angular.module('app', ['ionic']);

Which has the following controller
app.controller('ContactCtrl', function($scope, Contacten) {
///snip
   $scope.createContact = function(contactName){
      var newContact = {naam: contactName};
      $scope.contacten.push(newContact);
      Contacten.save($scope.contacten);
      $scope.sideMenuController.close(); <-- This function is from the ionic dependency
      return newContact;
   };

I wrote the following test
$describe('ContactCtrl createContact add a contact', function() {
  beforeEach(module('ionic'));  
  beforeEach(module('app'));  

  it('It adds a contact to the array', inject(function(Contacten, $rootScope, $controller)   {
    var scope = $rootScope.$new(),
    ctrl = $controller("ContactCtrl", {$scope: scope});

    window.localStorage.clear();
    scope.contacten = [];

    var testContact = {name: "TestPerson" };
    scope.createContact(testContact);
    console.log(scope.contacten)
    expect(Contacten.all().length).toEqual(1);      
    }));
});

This throws the following error:
TypeError: 'undefined' is not an object (evaluating '$scope.sideMenuController.close')

Why doesn't the scope in the test file have access to this function while the scope in the controller has no problem accessing this?

Could you try to instantiate your controller and run a $scope.$digest() in a beforeEach?
