角度模拟依赖函数

时间:2014-03-05 12:53:07

标签: angularjs jasmine

我有以下角度模块

var app = angular.module('app', ['ionic']);

其中有以下控制器

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;
   };

我写了以下测试

$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);      
    }));
});

这会引发以下错误:

TypeError: 'undefined' is not an object (evaluating '$scope.sideMenuController.close')

为什么测试文件中的作用域不能访问此函数,而控制器中的作用域访问它时没有问题?

0 个答案:

没有答案