如何在 Angular 单元测试中初始化 HTML 中定义的 var
How to initialize a var defined in HTML in Angular unit test
我在 component.ts 文件中声明了一个变量
declare var foo
此变量在 index.html 中定义为 <script>var foo = {}</script>
如何在Angular单元测试
中模仿index.html的声明
我是 运行 一个简单的单元测试,期望组件是真实的,
beforeEach(() => { fixture = TestBed.createComponent(FooComponent); component= fixture.componentInstance; fixture.detectChanges(); } it("should create", () => { expect(component).toBeTruthy; }) )
但是我收到错误 ReferenceError: foo is not defined
制作脚本并将其添加到 angular.json
您的区块应该类似于
"test": {
"builder": "@angular-devkit/build-angular:karma",
"options": {
"main": "src/test.ts",
"polyfills": "src/polyfills.ts",
"tsConfig": "tsconfig.spec.json",
"karmaConfig": "karma.conf.js",
"assets": ["src/favicon.ico", "src/assets"],
"styles": ["src/assets/style/main.scss"],
"scripts": [/* your script here */]
}
},
我在 component.ts 文件中声明了一个变量
declare var foo
此变量在 index.html 中定义为 <script>var foo = {}</script>
如何在Angular单元测试
中模仿index.html的声明我是 运行 一个简单的单元测试,期望组件是真实的,
beforeEach(() => { fixture = TestBed.createComponent(FooComponent); component= fixture.componentInstance; fixture.detectChanges(); } it("should create", () => { expect(component).toBeTruthy; }) )
但是我收到错误 ReferenceError: foo is not defined
制作脚本并将其添加到 angular.json
您的区块应该类似于
"test": {
"builder": "@angular-devkit/build-angular:karma",
"options": {
"main": "src/test.ts",
"polyfills": "src/polyfills.ts",
"tsConfig": "tsconfig.spec.json",
"karmaConfig": "karma.conf.js",
"assets": ["src/favicon.ico", "src/assets"],
"styles": ["src/assets/style/main.scss"],
"scripts": [/* your script here */]
}
},