简体   繁体   English

如何解决“模块不可用!” 对混合 Angular 应用程序进行单元测试时

[英]How to fix "Module is not available!" when unit testing a Hybrid Angular app

I have a hybrid angular app and unit testing with Karma-Jasmine but we hit a small problem.我有一个带有 Karma-Jasmine 的混合 Angular 应用程序和单元测试,但我们遇到了一个小问题。 I have a factory and component that is downgraded as an AngularJS app with:我有一个工厂和组件,它被降级为 AngularJS 应用程序:

angular
    .module('app.admin.branding')
    .factory('brandingFactory', downgradeInjectable(BrandingFactory));

angular
    .module('app.admin.branding')
    .directive('BrandNameComponent', downgradeComponent({
        component: BrandNameComponent
    }));

but it returns a Module 'app.admin.branding' is not available!但它返回一个Module 'app.admin.branding' is not available! upon running ng test在运行ng test

so the solution was adding brackets.所以解决方案是添加括号。

angular
    .module('app.admin.branding', [])

for both the component and factory.对于组件和工厂。 It works and the unit tests runs but when running on the browser and navigates to a page where the component is used an unknown error occurs and redirected to the main page.它可以工作并且单元测试会运行,但是当在浏览器上运行并导航到使用该组件的页面时,会发生未知错误并重定向到主页。

So we just removed the brackets for both classes as it affected our production app.所以我们只是删除了两个类的括号,因为它影响了我们的生产应用程序。 How do we fix our unit test error Module 'app.admin.branding' is not available or is there any workaround with the brackets approach so that it won't redirect?我们如何修复我们的单元测试错误Module 'app.admin.branding' is not available或者是否有任何使用括号方法的解决方法,以便它不会重定向?

Short answer;简短的回答;

Not quite sure about your setup, there might be a number of problems.不太确定你的设置,可能有很多问题。 The first thing to try is to use angular.mock.module , like so;首先要尝试使用angular.mock.module ,就像这样;

beforeEach(() => TestBed.configureTestingModule({}));
beforeEach(angular.mock.module('app.admin.branding'));

See documentation here请参阅此处的文档

Hopefully this is all that you need希望这就是你所需要的

Longer answer;更长的答案;

You may also have a problem with your "import chain".您的“导入链”也可能有问题。

As you have noticed the angularJS module gets created when you execute angular.module('app.admin.branding', []) .正如您所注意到的,当您执行angular.module('app.admin.branding', [])时,会创建 angularJS 模块。 When you append services, directives etc to this module you refer to it without the brackets.当您将服务、指令等附加到此模块时,您可以不带括号引用它。

When creating a hybrid app you need to be careful when creating the "import chain", ie the chain of import statements that link your app together.创建混合应用程序时,您需要小心创建“导入链”,即将您的应用程序链接在一起的导入语句链。

Below is the structure of a working angular hybrid created with angular-cli.下面是使用 angular-cli 创建的工作 angular 混合的结构。

混合应用架构

The app-ajs folder contains the angularJS app and each folder below this directory contains an index.ts file that imports all directives, services etc inside the folder. app-ajs 文件夹包含 angularJS 应用程序,该目录下的每个文件夹都包含一个 index.ts 文件,该文件导入文件夹内的所有指令、服务等。

The first index.ts file in our application looks like this;我们应用程序中的第一个 index.ts 文件如下所示;

// Vendor dependencies;
import './../../node_modules/adal-angular/dist/adal-angular.min';
import './../../node_modules/angular-cookies/angular-cookies.min';
// ...etc.

// App dependencies...
import './app.module';
import './appDependencies.module';
import './subfolder1';  // In reality this is an app area, just to show the idea!
import './subfolder2';

And an index.ts below a sub folder may look something like this;子文件夹下的 index.ts 可能看起来像这样;

import './some.module';
import './some.service';
import './some.directive';

In this case there would be a file named some.module.ts inside that folder that would contain the call to register to module (ie angular.module('some.module', []) )在这种情况下,该文件夹中将有一个名为some.module.ts文件,该文件将包含注册到模块的调用(即angular.module('some.module', [])

Creating this import chain is a bit tedious, but once you have it in place you can use it from both your main.ts and test.ts by importing it like this:创建这个导入链有点乏味,但是一旦你有了它,你就可以从你的main.tstest.ts使用它,像这样导入它:

import "./app-ajs";

This makes sure you run the same code in production as in your tests.这可确保您在生产中运行与测试中相同的代码。

Hope this helps.希望这可以帮助。

Starting with Angular 8, there is createAngularJSTestingModule .从 Angular 8 开始,有createAngularJSTestingModule See https://angular.io/api/upgrade/static/testing/createAngularJSTestingModule for an example.有关示例,请参阅https://angular.io/api/upgrade/static/testing/createAngularJSTestingModule

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

相关问题 单元测试混合角度/反应应用程序 - Unit testing a hybrid angular/react app 如何修复单元测试中的错误“不是函数” angular 11 - how to fix an error "is not a function" in unit testing angular 11 单元测试AngularJS:未捕获错误:[$ injector:nomod]模块不可用! 和ReferenceError:运行Karma时未定义模块 - Unit-Testing AngularJS: Uncaught Error: [$injector:nomod] Module is not available! and ReferenceError: module is not defined when run Karma 单位测试Angular时“angualar.module”和“module”之间的区别 - Difference between “angualar.module” and “module” when unit-testing Angular 使用业力测试失败:模块应用程序不可用 - testing using karma failed : Module app is not available 如何在角度单元测试中对服务进行单元测试? - How to unit test a service in angular unit testing? 使用downgradeComponent时,无法加载Bootstrapped Angular 2 + Angular 1 Hybrid App - Bootstrapped Angular 2 + Angular 1 Hybrid App not loading when using downgradeComponent Angular:如何在 angular 单元测试中使用路由器 - Angular: How to use router in angular unit testing 如何为多个需求模块设置单元测试 - How to setup unit testing for multiple require module 单元测试-何时/如何存根? - Unit testing - when/how to stub?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM