如何测试方法的返回值 [Jasmine]

How to test the returned value of a method [Jasmine]

我是使用 Jasmine 和 Karma 进行单元测试的新手。我的问题与此非常相似:

但我的不同,因为我想检查 returned 值的长度而不是值本身。我有一个将 n 作为数字的方法。如果数字小于 10,则该方法应该 return 相同的数字,但前面的 0 作为字符串。有效范围是 0 到 12(实际上它是月份选择器组件的一部分。我正在尝试实现 MM 格式。)。例如,如果输入 5,则 returned 值应为 05 作为字符串。这是代码:

convertToDoubleDigit(n) {
    n = String(n);
    if (n.length === 1) {
        n = '0' + n;
    }
    return n;
}

我的测试用例是:

it('should call convertToDoubleDigit and convert to double digit', () => {
    fixture.componentInstance.convertToDoubleDigit(2);
    expect(fixture.componentInstance.length) <----- This is what I'm asking
})

我知道我应该这样做:

expect(fixture.componentInstance......length).toBe(2)

请帮我测试这部分。我无法跳出框框思考。

您不应该检查长度,但是 instead of value here would valid test case:

it('should call convertToDoubleDigit and convert to double digit', () => {
    fixture.componentInstance.convertToDoubleDigit(2);
    expect(fixture.componentInstance.value).toEqual(02)
})