【发布时间】:2019-08-08 08:58:58
【问题描述】:
我正在尝试为使用自定义编写的管道的组件编写测试,但我从业力中得到错误,我没有正确提供它。项目正在构建/工作且没有错误(似乎所有内容都正确提供),但对于测试,似乎有些问题。出了什么问题以及如何解决? :? (某些组件仅在 html 中使用它,其中一些在服务等中使用,因此似乎问题不在于 providers: [AppTranslatePipe]
Failed: StaticInjectorError(DynamicTestModule)[AppTranslatePipe ->
TranslateService]:
StaticInjectorError(Platform: core)[AppTranslatePipe -> TranslateService]:
NullInjectorError: No provider for TranslateService!
.html(我使用这个管道的部分)
<button
mat-raised-button
mat-dialog-close
class="close">
{{ 'common.cancel' | appTranslate }}
</button>
.module 导入管道的模块,因为我在html上使用它
@NgModule({
declarations: [
UserSettingsComponent
],
imports: [
CommonModule,
MatDialogModule,
MatFormFieldModule,
MatInputModule,
MatButtonModule,
MatSelectModule,
AppTranslateModule,
],
exports: [
UserSettingsComponent,
]
})
export class UserSettingsModule { }
.component.ts(因为我也在 showNotificationOnSubmit 函数中使用了这个管道,我需要通过 providers 设置 AppTranslatePipe,这可能是问题吗?:?
@Component({
selector: 'lib-user-settings',
templateUrl: './user-settings.component.html',
styleUrls: ['./user-settings.component.sass'],
providers: [ AppTranslatePipe ]
})
export class UserSettingsComponent {
constructor(
public dialogRef: MatDialogRef<UserSettingsComponent>,
private translationService: TranslationService,
private globalNotificationsService: GlobalNotificationsService,
private appTranslatePipe: AppTranslatePipe,
) { }
showNotificationOnSubmit(
isNewLangSetted: boolean,
isNewPasswordSetted: boolean
): void {
this.globalNotificationsService.addTypedNotification(
this.appTranslatePipe.transform('user-settings.success.update'),
NotificationTypeEnum.success
);
}
}
*spec.ts
fdescribe('UserSettingsComponent', () => {
let component: UserSettingsComponent;
let fixture: ComponentFixture<UserSettingsComponent>;
let translationService: jasmine.SpyObj<TranslationService>;
let appTranslatePipe: jasmine.SpyObj<AppTranslatePipe>;
let userService: jasmine.SpyObj<UserService>;
let globalNotificationsService: jasmine.SpyObj<GlobalNotificationsService>;
const fb: FormBuilder = new FormBuilder();
let matDialogRef: jasmine.SpyObj<MatDialogRef<UserSettingsComponent>>;
const MOCK_TRANSLATIONS: ILocale[] = [
{
Id: 0,
Key: 'key',
Snapshot: null,
Title: 'title-1',
TotalCount: 0
}
];
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [
UserSettingsComponent,
],
imports: [
LoaderModule,
BrowserAnimationsModule,
ReactiveFormsModule,
FormsModule,
MatDialogModule,
MatFormFieldModule,
MatInputModule,
MatButtonModule,
MatSelectModule,
AppTranslateModule,
],
providers: [
{
provide: TranslationService,
useValue: jasmine.createSpyObj('TranslationService', [
'getTranslationList',
'updateLanguage',
'setLanguage'
])
},
{
provide: UserService,
useValue: jasmine.createSpyObj('UserService', [
'updatePassword',
])
},
{
provide: GlobalNotificationsService,
useValue: jasmine.createSpyObj('GlobalNotificationsService', [
'addTypedNotification',
])
},
{ provide: FormBuilder, useValue: fb },
{
provide: MatDialogRef,
useValue: jasmine.createSpyObj('MatDialogRef', [
'close'
]),
},
{
provide: AppTranslatePipe,
useValue: jasmine.createSpyObj('AppTranslatePipe', [
'transform'
]),
},
]
})
.compileComponents();
translationService = TestBed.get(TranslationService);
appTranslatePipe = TestBed.get(AppTranslatePipe);
userService = TestBed.get(UserService);
globalNotificationsService = TestBed.get(GlobalNotificationsService);
matDialogRef = TestBed.get(MatDialogRef);
fixture = TestBed.createComponent(UserSettingsComponent);
component = fixture.componentInstance;
}));
fit('should create', () => {
translationService.getTranslationList.and.returnValue(
of([])
);
fixture.detectChanges();
expect(component).toBeTruthy();
expect(translationService.getTranslationList)
.toHaveBeenCalled();
});
});
管道本身:
import { PipeTransform, Pipe } from '@angular/core';
import { TranslateService } from '@ngx-translate/core';
import { TranslationService } from '../../services/translation/translation.service';
import { ILocale } from '../../models/locale.model';
@Pipe({
name: 'appTranslate',
pure: false
})
export class AppTranslatePipe implements PipeTransform {
constructor(
private translationService: TranslationService,
private translate: TranslateService,
) { }
transform(
fileKey: string,
dbKey?: string
) {
if (!fileKey) {
return '';
}
const currentLang: ILocale = this.translationService.getLanguage();
if (currentLang) {
const dbTranslation: string = this.translationService.getDBTranslation_byKey(dbKey);
if (dbKey && dbTranslation) {
return dbTranslation;
} else {
return this.translate.instant(fileKey) + '\`';
}
} else {
return this.translate.instant(fileKey) + '\`';
}
}
}
【问题讨论】:
-
管道在
declarations中提及,而不是providers。 -
@user 你是什么意思?如果我在模块中添加为
provider,那么我会收到错误消息Failed: Template parse errors: The pipe 'appTranslate' could not be found (" -
嗯,它需要在
providers数组中,因为不幸的是,管道似乎被注入了一些组件中,而不仅仅是在 html 中使用。问题是翻译服务没有模拟
标签: angular unit-testing karma-jasmine angular8