如何在 Angularjs 控制器之外测试函数



我尝试在angularjs中测试控制器旁边的函数,但总是失败。

这是针对 AngularJS 项目的

//控制器---

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

//-----规范----

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

茉莉花错误:预期已调用间谍my_controller.first_function

在你的例子中,你从来没有调用first_function,所以茉莉花是正确的,它没有被调用。

最新更新