How to deal with angular module's config function when unit testing?

╄→尐↘猪︶ㄣ 提交于 2019-12-28 15:09:27

问题


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?

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


回答1:


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

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


来源:https://stackoverflow.com/questions/19287223/how-to-deal-with-angular-modules-config-function-when-unit-testing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!