[英]Angular component (with a directive) testing
我有一個要測試的組件。 測試失敗並顯示消息
AdminPanelComponent > 應該創建
TypeError:無法讀取未定義的屬性“角色”
錯誤屬性: Object({ ngDebugContext: DebugContext_({ view: Object({ def: Object({ factory: Function, nodeFlags: 50577443, rootNodeFlags: 1, nodeMatchedQueries: 0, flags: 0, 節點: [ Object({ nodeIndex: 0 , parent: null, renderParent: null, bindingIndex: 0, outputIndex: 0, checkIndex: 0, flags: 1, childFlags: 50577443, directChildFlags: 1, childMatchedQueries: 0, matchedQueries: Object({ }), matchedQueryIds: 0, references :對象({}),ngContentIndex:null,childCount:11,綁定:[],bindingFlags:0,輸出:[],元素:對象({ ns:'',名稱:'div',attrs:[數組] , template: null, componentProvider: null, componentView: null, componentRendererType: null, publicProviders: null({ }), allProviders: null({ }), handleEvent: Z86408593C34AF77FDD90D F932F8B5261Z }), provider: null, text: null, query: null, ngContent: null }), Object({ nodeIndex: 1, parent: Object({ nodeIndex: 0, parent: null, renderParent: null, bindingIndex: 0, outputIndex: 0, checkIndex: 0, flags: 1, childFlags: 50577443, directChildFlags: 1, chi... at HasRoleDirective.ngOnInit ( http://localhost:9876/_karma_webpack_/src/app/_directives/hasRole.directive。 ts:16:53 ) at checkAndUpdateDirectiveInline ( http://localhost:9876/_karma_webpack_/node_modules/@angular/core/fesm2015/core.js:26276:1 ) at checkAndUpdateNodeInline ( http://localhost:9876/_karma_webpack_/node_modules /@angular/core/fesm2015/core.js:37133:1 ) 在 checkAndUpdateNode ( http://localhost:9876/_karma_web pack_/node_modules/@angular/core/fesm2015/core.js:37072:1 ) 在 debugCheckAndUpdateNode ( http://localhost:9876/_karma_webpack_/node_modules/@angular/core/fesm2015/core.js:38094:36 ) 在debugCheckDirectivesFn ( http://localhost:9876/_karma_webpack_/node_modules/@angular/core/fesm2015/core.js:38037:1 ) at Object.eval [as updateDirectives] (ng:///DynamicTestModule/AdminPanelComponent.ngfactory.js :62:5) at Object.debugUpdateDirectives [as updateDirectives] ( http://localhost:9876/_karma_webpack_/node_modules/@angular/core/fesm2015/core.js:38025:1 ) at checkAndUpdateView ( http://localhost: 9876/_karma_webpack_/node_modules/@angular/core/fesm2015/core.js:37037:1 ) 在 callViewAction ( http://localhost:9876/_karma_webpack_/node_modules/@angular/core/fesm2015/core.js:374031: )
管理面板.component.ts
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'app-admin-panel',
templateUrl: './admin-panel.component.html',
styleUrls: ['./admin-panel.component.css']
})
export class AdminPanelComponent implements OnInit {
constructor() { }
ngOnInit() {
}
}
管理面板.component.html
<div class="container mt-5">
<h2>Admin panel</h2>
<div class="tab-panel">
<tabset class="member-tabset">
<tab heading="User Management" *appHasRole="['Admin']">
<div class="container">
<app-user-management></app-user-management>
</div>
</tab>
<tab heading="Photo management" *appHasRole="['Admin', 'Moderator']">
<app-photo-management></app-photo-management>
</tab>
</tabset>
</div>
</div>
hasRole.directive.ts
import { Directive, Input, ViewContainerRef, TemplateRef, OnInit } from '@angular/core';
import { AuthService } from '../_services/auth.service';
@Directive({
selector: '[appHasRole]'
})
export class HasRoleDirective implements OnInit {
@Input() appHasRole: string[];
isVisible = false;
constructor(private viewContainerRef: ViewContainerRef,
private templateRef: TemplateRef<any>,
private authService: AuthService) { }
ngOnInit() {
const userRoles = this.authService.decodedToken.role as Array<string>;
// if no roles clear the viewContainerRef
if (!userRoles) {
this.viewContainerRef.clear();
}
// if user has role need to render the element
if (this.authService.roleMatch(this.appHasRole)) {
if (!this.isVisible) {
this.isVisible = true;
this.viewContainerRef.createEmbeddedView(this.templateRef);
} else {
this.isVisible = false;
this.viewContainerRef.clear();
}
}
}
}
無法工作的代碼部分是
const userRoles = this.authService.decodedToken.role as Array<string>;
如何測試這個組件?
測試 angular 應用程序的最佳方法是模擬除您要測試的組件之外的所有內容。
最簡單的方法是使用像ng-mocks這樣的庫。
不過,您需要像那樣存根 AuthService
TestBed.configureTestingModule({
declarations: [HasRoleDirective, AdminPanelComponent],
provides: [
{
provide: AuthService,
useValue: {
decodedToken: {
role: [], // <- you can put here values for the test
},
roleMatch: () => false, // <- or to use a spy here.
},
},
],
})
在這種情況下,AuthService 將是一個帶有不做任何事情的假方法的存根。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.