如何应对角模块的配置功能时,单元测试? [英] How to deal with angular module's config function when unit testing?

查看:138
本文介绍了如何应对角模块的配置功能时,单元测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当使用噶/茉莉花角度应用程序中设置一个单元测试套件,它是建议包括在测试的文件,应用程序模块的配置函数的js?

When setting up a unit test suite for an angular application using Karma/Jasmine, is it recommended to include the js with the app module's config function in the test's files?

我读过,我们建议从测试排除这一点,但是,似乎尴尬,因为有这种情况发生在将$ P $的配置功能往往严重的设置从工作pvent应用程序。

I've read that it is suggested to exclude this from testing, however that seems awkward because there's often critical setup that happens in the config function that would prevent the application from working.

什么是解决这个问题的最佳做法?在嘲笑的方式创建一个模拟配置函数做同样的事情?

What's the best practice around this? Create a mock config function that does the same thing in a 'mocked' manner?

我碰到这个问题,运行自己,但想了解更广泛的战略:
如何做单元测试角转换

I'm running across this issue myself but want to understand the broader strategy: How do unit test with angular-translate

推荐答案

在我的申请,我结束了使用以下解决方案:

In my application, I ended up using the following solution:

定义一个的appBase模块,所有我想,当单位 - 运行配置运行功能测试并创建另一个应用程序模块,它宣称的appBase模块作为依赖,包括所有的配置运行功能,我不什么时候运行单元测试。然后我所有的单元测试使用的appBase模块,而最终的应用程序使用应用程序模块。在code:

Define an "appBase" module with all the config and run functions that I want to run when unit-testing and create another "app" module which declares "appBase" module as a dependency and includes all the config and run functions that I don't what to run when unit-testing. Then all my unit tests use the "appBase" module, while the final application uses the "app" module. In code:

angular.module('appBase', ['dependencies'])
       .config(function() {
            // This one will run when unit-testing. Can also set-up mock data
            // that will later be overridden by the "app" module
        });

angular.module('app', ['appBase'])
       .config(function() {
            // This function will only run in real app, not in unit-tests.
       });

这篇关于如何应对角模块的配置功能时,单元测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆