如何測試控制台值以滿足單元測試? (How to test console value to satisfy unit test?)


問題描述

如何測試控制台值以滿足單元測試? (How to test console value to satisfy unit test?)

對於我們正在組合的非營利類,我們正在嘗試驗證控制台值以查看函數是否成功執行。這應該是一個小而簡單的測試,但我碰壁了。:‑) 任何幫助表示讚賞。PS 如果新軟件包能讓事情變得更高效、更簡單,我們願意接受。

這是偽嘗試:

it('should match value in console', () => {
  const fixture = TestBed.createComponent(AppComponent);
  fixture.detectChanges();
  const compiled = fixture.debugElement.nativeElement;
  expect(compiled.console.log.textContent).toEqual(
    'CONSOLE MESSAGE',
  );
});

參考解法

方法 1:

If you need to unit test your console log, you can try something like this:‑

it('test', () => {
  const fixture = TestBed.createComponent(AppComponent);
  component = fixture.componentInstance;
  fixture.detectChanges();
  spyOn(console, 'log');
  component.methodToBeTested(); // call the method which has console.log
  expect(console.log).toHaveBeenCalledWith('log message'); // assuming console.log('log message') is in methodToBeTested()
});

(by DevvvvvAnand Bhushan)

參考文件

  1. How to test console value to satisfy unit test? (CC BY‑SA 2.5/3.0/4.0)

#Angular #unit-testing #jasmine






相關問題

量角器,根據 API 請求使用 angular2 模擬後端 (Protractor, mocking backend with angular2 on api request)

XMLHttpRequest 響應:預檢請求未通過訪問控制檢查: (XMLHttpRequest response: preflight request does not pass the access control check:)

為 index.html 中的 src 標籤指定前綴 (Specify a prefix for the src tag in index.html)

模型更改後模板未更新 (Template not updating after model changed)

如何在 Ionic 4 firebase PWA 應用程序中將 promise 轉換為 observable (how do I convert a promise to observable in a Ionic 4 firebase PWA app)

如何以角度顯示漂亮的html (How to display pretty html in angular)

如何保持 Angular 組件的最新狀態? (How to keep the latest state of Angular components?)

在 mat-grid-tile Angular 上顯示 flex (display flex on mat-grid-tile Angular)

如何使用地圖構建離線 Electron 應用程序 (How to build an Offline Electron app with maps)

如何測試控制台值以滿足單元測試? (How to test console value to satisfy unit test?)

將表情符號插入 Angular 的輸入字段 (Insert emoticon into the input field in Angular)

如何在 Angular 中導入“數字”:“^1.2.6”? (How to import "numeric": "^1.2.6" in Angular?)







留言討論