我是 angular 的新手,試圖為下面的函式撰寫單元測驗用例。
HomeComponent.ts
ngOnInit() {
this.msalBroadcastService.inProgress$
.pipe(
filter((status: InteractionStatus) => status === InteractionStatus.None),
takeUntil(this._destroying$)
)
.subscribe(() => {
this.YesNo = [
{ label: 'Yes', value: 'Yes' },
{ label: 'No', value: 'No' },
];
this.cols = [
{ field: 'Human', header: 'name', width: '5' },
{ field: 'MaritalStatus', header: 'Marital Status', width: '8' },
];
this.getAllAccounts();
});
}
我嘗試了下面的測驗用例,但我不知道如何覆寫YesNo、cols、getAllAccounts ()
HomeComponent.spec.ts
class MockUserService {
inProgress = of("Login");
}
describe('AccountComponent', () => {
let UserService;
let comp;
let userService;
let testInteractionStatus: InteractionStatus.None;
beforeEach(() => {
TestBed.configureTestingModule({
providers: [
{
provide: UserService,
useClass: MockUserService
},
MsalService,
MsalGuard,
MsalBroadcastService,
MessageService,
HomeService,
HomeComponent
],
imports: [HttpClientTestingModule, RouterTestingModule],
declarations: [
HomeComponent
],
}).compileComponents();
});
it('should test....', () => {
const fixture = TestBed.createComponent(HomeComponent);
const app = fixture.debugElement.componentInstance;
app.ngOnInit();
fixture.detectChanges();
expect(userService.inProgress.YesNo).toBeDefined();
expect(userService.inProgress.YesNo).toBeDefined();
});
- 角度版本
Angular CLI:12.2.6 節點:14.17.6 包管理器:npm 7.23.0 作業系統:darwin x64
uj5u.com熱心網友回復:
您可以使用類似 npm 的rxjs-marbles或jasmine- marbles來測驗從您的 observable 發出的不同值。
在測驗套件中,您可以訂閱您的 observable。一旦 observable 發出,您將執行以下操作:
expect(component.yesNo).toHaveLength(2) // this is pseudo code, actual code may vary
請注意,當前您正在測驗 home 組件,但是在 home 組件的測驗中,您希望服務設定一些東西。單元測驗是關于隔離外部依賴項,以便您只測驗組件本身。
所以你不要期望服務價值是什么。相反,您模擬服務并期望組件值成為某些東西。
關于測驗,getAllAccounts()您將創建一個間諜并期望它已被呼叫。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/360176.html
上一篇:NoSuchMethodError:org.apache.hadoop.conf.Configuration.getPassword
下一篇:如何出于測驗目的雙重匯入模塊?
