如何测试一个模块的运行程序段模块、测试、程序

2023-09-13 02:36:18 作者:切、伱全家都到碗裏去

我希望我的AngularJS的应用程序,使HTTP请求从服务器获取用户信息或重定向到登录界面。我在应用程序的主模块的运行块中实现这一点。但我怎么测试在运行块中的code?或者我应该提出这个初始化code控制器中,使其可测试?我在写我的噶和Jasmine测试。

I want my AngularJS app to make a http request to retrieve user information from the server or redirect to login screen. I implemented this in a run block of my app's main module. But how do I test the code in a run block? Or should I move this initialization code in a controller to make it testable? I'm writing my tests with Karma and Jasmine.

任何帮助将是AP preciated!

Any help would be appreciated!

推荐答案

您可以通过模块的_runBlocks财产获得参考运行块。

You can get the reference to the run block via the _runBlocks property of the module.

与您的模块$ C $文件C:

The file with your module code:

angular.module('xmpl', []).
  run(function() {
    // some code here
  });

测试套件:

var myModule = angular.module('xmpl'),
    runBlock = myModule._runBlocks[0];
// Now can test the runBlock function according to your scenario