我正在测试的组件之一是导入位于/src
文件夹外部的常量文件。运行测试时,component.ts
文件中有一个错误,指出常量对象未定义。以下是详细信息。任何帮助都会非常有帮助。预先感谢。
这是我运行ng test
时遇到的错误。
错误:
TypeError:无法读取未定义的属性“ ROUTE_SERVICE_USER” 在WelcomeComponent ../ src / app / welcome / welcome.component.ts.WelcomeComponent.ngOnInit(http://localhost:9876/src/app/welcome/welcome.component.ts?:24:30)
文件夹结构:
AppConstants.js:
const AppConstants = {
HEADER_LANG: 'Content-Language',
HEADER_LANG_LOWER: 'content-language',
ROUT_WELCOME: '/welcome',
ROUT_NEWUSER: '/newuser',
ROUTE_SERVICE_USER: '/service/user/registered',
};
module.exports = AppConstants;
Welcome.component.ts:
import { Component, OnInit } from '@angular/core';
import AppConstants from '../../../common/AppConstants.js';
@Component({
selector: 'app-welcome',
templateUrl: './welcome.component.html',
styleUrls: ['./welcome.component.css']
})
export class WelcomeComponent implements OnInit {
constructor() {
}
ngOnInit() {
console.log(AppConstants.ROUTE_SERVICE_USER);
}
}
Welcome.component.spec.ts:
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { WelcomeComponent } from './welcome.component';
describe('WelcomeComponent', () => {
let component: WelcomeComponent;
let fixture: ComponentFixture<WelcomeComponent>;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ WelcomeComponent ]
})
.compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(WelcomeComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
});