簡體   English   中英

如何對函數的返回值進行單元測試 - Angular (Jasmine/Karma)

[英]How to unit test return value of function - Angular (Jasmine/Karma)

我想知道是否有一種方法可以正確測試 Angular 中函數的返回值。 我想基本上測試一個測試的返回值是否為真,並編寫另一個測試相反的場景。

Ts 組件:

    get() {
        if (this.object == undefined) {
            return true;
        } else {
            return false;
        }    
    }

我現在認為適合測試返回值的唯一方法是設置一個變量來保存返回值。 下面是我的嘗試,我只是堅持斷言是預期的。

測試:

it('should call get function and return true', () => {
        //Arrange
        component.object = undefined;

        //Act
        component.get();

        //Assert
        expect(component.get). <-- *stuck here*

    });

獲取行為中的值,然后在assert 中檢查它是否為true

it('should call get function and return true', () => {
        // Arrange
        component.object = undefined;

        // Act
        var result = component.get();

        // Assert
        expect(result).toBe(true);
    });

附帶說明一下, get方法的主體可以簡化為

return this.object === undefined;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM