Javascript 如何从不同的控制器测试$scope中的函数?

Javascript 如何从不同的控制器测试$scope中的函数?,javascript,angularjs,jasmine,Javascript,Angularjs,Jasmine,我正在使用Jasmine测试我的AngularJS应用程序。我有一个authenticationon控制器,它调用我在应用程序控制器的作用域中定义的函数。因此: 1。AppController $scope.setUser = function() {} $scope.setUser(User); 2。AuthenticationController $scope.setUser = function() {} $scope.setUser(User); 我正在测试Authenticat

我正在使用Jasmine测试我的AngularJS应用程序。我有一个authenticationon控制器,它调用我在应用程序控制器的作用域中定义的函数。因此:

1。AppController

$scope.setUser = function() {}
$scope.setUser(User);
2。AuthenticationController

$scope.setUser = function() {}
$scope.setUser(User);
我正在测试
AuthenticationController
,setUser()不在
AuthenticationController
范围内。如何从
AppController
注入作用域/函数

错误消息:

TypeError: $scope.setUser is not a function
angular
  .module( 'authentication', [])
  .controller('AuthenticationController', AuthenticationController);

AuthenticationController.$inject = ['$scope', '$rootScope', 'AUTH_EVENTS', 'AuthenticationService'];
angular
  .module('dashboard', [
    'authentication'
  ])
  .run(run);
我应该嘲笑整个函数吗?建筑有臭味吗?最好的方法是什么

编辑: 在真正的应用程序中,AuthenticationController被注入到我的仪表板应用程序中

认证控制器:

TypeError: $scope.setUser is not a function
angular
  .module( 'authentication', [])
  .controller('AuthenticationController', AuthenticationController);

AuthenticationController.$inject = ['$scope', '$rootScope', 'AUTH_EVENTS', 'AuthenticationService'];
angular
  .module('dashboard', [
    'authentication'
  ])
  .run(run);
仪表板:

TypeError: $scope.setUser is not a function
angular
  .module( 'authentication', [])
  .controller('AuthenticationController', AuthenticationController);

AuthenticationController.$inject = ['$scope', '$rootScope', 'AUTH_EVENTS', 'AuthenticationService'];
angular
  .module('dashboard', [
    'authentication'
  ])
  .run(run);

信息:在我的问题中更改了名称,以便于理解。

将$rootScope注入控制器

  • AppController:

    $rootScope.setUser=function(){}

  • 身份验证控制器

    $rootScope.setUser()


  • AuthenticationController在真正的应用程序中如何访问该作用域?在这种情况下,我通常会模拟该作用域。@Bimper good point。我正在我的主模块仪表板中注入AuthenticationController。所以我猜通过这个注入它可以访问?@0xc0de你能给出一个链接或代码示例来说明如何做到这一点吗?我在问自己,如果我嘲笑这一点,我如何才能确保一切都以正确的方式链接/注入?如果测试似乎“困难”,那么代码体系结构可能会有所不同。