1

这个问题似乎是相同的,但绝对不是重复的。

在我的一个函数中,我正在调用this.cordovaFile.readAsArrayBuffer(this.cordovaFile.dataDirectory, key)

cordovafile.readAsArrayBuffer()从 ipad 文件存储中读取数据,但在单元测试中我不能这样做,因为它在浏览器中运行,所以我使用 SpyOn 来获取假值,我很难做到这一点。

下面有人可以帮我吗?

    private getTimesheetByKey(key: TIMESHEET_KEYS): Observable<TimesheetModel> {
        let timesheet: TimesheetModel;

        return from(
            this.cordovaFile
                .readAsArrayBuffer(this.cordovaFile.dataDirectory, key)
                .then(compressedConfirmation => {
                    const start = moment();
                    const uint8Array = new Uint8Array(compressedConfirmation);
                    const jsonTimeSheet = this.LZString.decompressFromUint8Array(uint8Array);

                    timesheet = new TimesheetModelFromJson(<JsonTimesheetModel>(
                        JSON.parse(jsonTimeSheet)
                    ));
                    this.saveTimesheetByKey(key, timesheet);
                    return timesheet;
                })
                .catch((error: Error) => {})
        );
    }

这就是我要编写的单元测试的方式,我不知道如何 SpyOnthis.cordovaFile.readAsArrayBuffer(this.cordovaFile.dataDirectory, key)并返回值

it('should save the Timesheet to file storage', () => {
        spyOn(LocalStorageTimesheetService, 'getTimesheetByKey')
            .and.callThrough(cordovaFile.readAsArrayBuffer())
            .and.returnValue(timesheet);

        expect(timesheet).toEqual();
    });

this.cordovaFile.readAsArrayBuffer()应该返回类似下面的假值

timesheet = {
            startOfWork: '2019-07-02T02:00:00.000Z',
            notifications: [],
            additionalExpenses: [],
            manualTimesheetEntries: [],
            endOfWork: undefined,
            isSubmitted: false,
            attendanceType: 'FREE',
        };
4

1 回答 1

1

我认为spyOn(LocalStorageTimesheetService, 'getTimesheetByKey')不会起作用,因为getTimesheetByKey它是私人功能。先做吧public

此外,通过spyingreadAsArrayBuffer您将能够控制结果是compressedConfirmation和不是timesheettimesheet在执行new Uint8Array() etc etc 操作后计算compressedConfirmation

如果您只是担心在我们调用时检查该值是否已保存,getTimesheetByKey那么您可以这样编写:

it('should call "saveTimesheetByKey()" when getTimesheetByKey() is called', () => {
    const compressedConfirmationMock = 'some_val'; // expected Mock value of compressedConfirmation
    spyOn(LocalStorageTimesheetService.cordovaFile, 'readAsArrayBuffer').and.returnValue(Promise.resolve(compressedConfirmationMock));
    spyOn( LocalStorageTimesheetService, 'saveTimesheetByKey').and.callThrough();
    LocalStorageTimesheetService.getTimesheetByKey(SOME_VAL_OF_TYPE_TIMESHEET_KEYS);
    expect(LocalStorageTimesheetService.saveTimesheetByKey).toHaveBeenCalled();
});


it('should save the Timesheet to file storage using saveTimesheetByKey()', () => {
    // here unit test the logic of saveTimesheetByKey() function 
    // which is actually saving the data. 
})


it('should perform some logic where getTimesheetByKey() is called', () => {
   // improve the "it" statement. 
   spyOn(LocalStorageTimesheetService, 'getTimesheetByKey')
        .and.returnValue(of({
            startOfWork: '2019-07-02T02:00:00.000Z',
            notifications: [],
            additionalExpenses: [],
            manualTimesheetEntries: [],
            endOfWork: undefined,
            isSubmitted: false,
            attendanceType: 'FREE',
        }));
   // and here write unit test that function which is using value of `timesheet` returned by calling "getTimesheetByKey()"
   //  expect block here
})

请注意我将您的单元测试逻辑分解为 3 个不同块的方式。单元测试应该是更加独立的测试,并且应该写在函数逻辑的粒度级别上。

于 2019-07-16T16:40:09.420 回答