简体   繁体   English

如何模拟角度测试模块

[英]How to mock modules for angular testing

Lets say i have an angular module defined as 让我们说我有一个角度模块定义为

var app = angular.module('app',[dependenceny1Module,dependenceny2Module,dependenceny3Module......])

where app is a module which depends on bulk of other modules. app是一个依赖于大量其他模块的模块。 Now for unit testing i can mock the module using 现在进行单元测试我可以使用模拟模块

mock('app') 

but i have to create mock modules for all dependency modules like below 但我必须为所有依赖模块创建模拟模块,如下所示

mockDependency1 = angular.module('dependency1Module')
mockDependency1 = angular.module('dependency2Module')

Or i have to load all the script files containing those modules. 或者我必须加载包含这些模块的所有脚本文件。

I am wondering whats the best approach to mock out the dependency modules here? 我想知道最好的方法来模拟依赖模块吗? especially when dependency modules are too many. 特别是当依赖模块太多时。

Look into requireJs to load your dependencies. 查看requireJs以加载您的依赖项。 With RequireJs, you can load different files for your production code and your test code. 使用RequireJs,您可以为生产代码和测试代码加载不同的文件。

Here is a (basic) example. 这是一个(基本)例子。

Let's say you have a javascript file with defines your angular module (named eg app.js ): 假设您有一个javascript文件,用于定义您的角度模块 (名为app.js ):

define(['dependency1Module', 'dependency2Module'], function(dependency1Module, dependency2Module) {
   var app = angular.module('app',['dependenceny1Module', 'dependenceny2Module']);
   return app;
}

Modules dependency1Module, dependency2Module, ... have a similar setup: 模块dependency1Module,dependency2Module,...有类似的设置:

define(function() {
   var module = angular.module('dependenceny1Module');
   return module;
}

Now you need a bootstrap file (named eg bootstrap.js to define the location of the files. So you'll have two bootstrap files: one your your production code (using possible minified versions of some libraries) and a version for test purposes: 现在你需要一个bootstrap文件(名为bootstrap.js来定义文件的位置。所以你将有两个引导程序文件:一个是你的生产代码(使用某些库的可能的缩小版本)和一个用于测试目的的版本:

require.config({
  baseUrl: "path/to/production|test scripts",
  paths: {
    angular: 'path/to/angular',
    jquery: 'path/to/jquery',
  },
  shim: {
    angular: {
      exports: 'angular',
      deps: ['jquery']
    }
  }
});

require(["angular", "app", ], function(angular, app) {
  angular.element(document).ready(function() {
    angular.bootstrap(document, ['app']);
  });
});

Instead of changing the baseUrl , you could define different paths for the modules you want to mock: 您可以为要模拟的模块定义不同的路径,而不是更改baseUrl

paths: {
   dependency1Module: 'path/to.mock/dependency1Module'
}

Last step is to plugin the bootstrap file in your html code (production or test): 最后一步是在你的html代码(生产或测试)中插入bootstrap文件:

<script type="text/javascript" src="path/to/require.js"></script>
<script type="text/javascript" src="path/to/bootstrap(production|test).js"></script>

This is a basic requirejs setup. 这是一个基本的requirejs设置。 Of course, before you can use a certain angular type (eg controller, service, ...), you'll need to setup a requirejs module for it too (and require it eg in your app.js ). 当然,在你可以使用某种角度类型(例如控制器,服务......)之前,你还需要为它设置一个requirejs模块(例如在你的app.js require它)。

Modularizing your own app will help. 模块化您自己的应用程序将有所帮助。

If your app looks like this: 如果您的应用看起来像这样:

var app = angular.module('app',[dependenceny1Module, dependenceny2Module, dependenceny3Module......])

and contains 50 things like filters, directives and controllers. 并包含50个过滤器,指令和控制器等内容。 Some of them depend on dependenceny1Module and some of them depend on dependenceny2Module. 其中一些依赖依赖尼特模块,其中一些依赖依赖尼姆模块。

Break them apart based on their responsibilities and dependencies. 根据他们的责任和依赖性将他们分开。

var mymodule1 = angular.module('mymodule1',[dependenceny2Module, dependenceny3Module])

var mymodule2 = angular.module('mymodule2',[dependenceny1Module,])

Then make your app depend on your own modules: 然后让您的应用程序依赖于您自己的模块:

var app = angular.module('app',[ mymodule1, mymodule2])

Your app should have mostly bootstrap code. 您的app应该包含大部分引导代码。

Now when you want to test controller13 of mymodule2 , you will only have to mock mymodule2 's dependencies if that module has dependencies. 现在,当你想测试mymodule2 controller13时, 如果该模块有依赖关系,你只需要模拟mymodule2的依赖关系。 So test suites will not load app they will only load the module they test. 因此测试套件不会加载app ,只会加载他们测试的模块。

See this example where the tests for MyMainCtrl1 of mymodule1 only care about s1 from external1 even if the app as a whole depends on external1 , external2 and external3 . 请参阅此示例 ,其中mymodule1 MyMainCtrl1测试仅关注来自external1 s1 ,即使app整体依赖于external1external2external3

It all depends on what you actually want to test, if you are unit testing you want load/mock as little as possible. 这一切都取决于你真正想要测试的内容,如果你是单元测试,你想尽可能少地加载/模拟。 For example, if you unit test a controller, you mock the module and only load/mock whatever dependencies it uses. 例如,如果您对控制器进行单元测试,则模拟模块并仅加载/模拟它使用的任何依赖项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM