如何在函数内部监视函数和 return 假值
How to SpyOn function inside Function and return Fake value
这个问题似乎是一样的,但绝对不是重复的。
在我的一个函数中,我正在调用 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) => {})
);
}
这就是我要写的单元测试的方式,我不知道如何监视 this.cordovaFile.readAsArrayBuffer(this.cordovaFile.dataDirectory, key)
和 return 的值
it('should save the Timesheet to file storage', () => {
spyOn(LocalStorageTimesheetService, 'getTimesheetByKey')
.and.callThrough(cordovaFile.readAsArrayBuffer())
.and.returnValue(timesheet);
expect(timesheet).toEqual();
});
this.cordovaFile.readAsArrayBuffer()
应该 return 假值 如下所示
timesheet = {
startOfWork: '2019-07-02T02:00:00.000Z',
notifications: [],
additionalExpenses: [],
manualTimesheetEntries: [],
endOfWork: undefined,
isSubmitted: false,
attendanceType: 'FREE',
};
我认为 spyOn(LocalStorageTimesheetService, 'getTimesheetByKey')
行不通,因为 getTimesheetByKey
是私有函数。先做public
此外,通过 spying
和 readAsArrayBuffer
,您将能够控制 compressedConfirmation
而不是 [=17] 的结果=]. timesheet
是在对 compressedConfirmation
执行 new Uint8Array()
etc etc 操作后计算得出的
如果您担心我们调用 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 个不同块的方式。单元测试应该是更孤立的测试,应该在功能逻辑的粒度级别上编写。
这个问题似乎是一样的,但绝对不是重复的。
在我的一个函数中,我正在调用 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) => {})
);
}
这就是我要写的单元测试的方式,我不知道如何监视 this.cordovaFile.readAsArrayBuffer(this.cordovaFile.dataDirectory, key)
和 return 的值
it('should save the Timesheet to file storage', () => {
spyOn(LocalStorageTimesheetService, 'getTimesheetByKey')
.and.callThrough(cordovaFile.readAsArrayBuffer())
.and.returnValue(timesheet);
expect(timesheet).toEqual();
});
this.cordovaFile.readAsArrayBuffer()
应该 return 假值 如下所示
timesheet = {
startOfWork: '2019-07-02T02:00:00.000Z',
notifications: [],
additionalExpenses: [],
manualTimesheetEntries: [],
endOfWork: undefined,
isSubmitted: false,
attendanceType: 'FREE',
};
我认为 spyOn(LocalStorageTimesheetService, 'getTimesheetByKey')
行不通,因为 getTimesheetByKey
是私有函数。先做public
此外,通过 spying
和 readAsArrayBuffer
,您将能够控制 compressedConfirmation
而不是 [=17] 的结果=]. timesheet
是在对 compressedConfirmation
new Uint8Array()
etc etc 操作后计算得出的
如果您担心我们调用 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 个不同块的方式。单元测试应该是更孤立的测试,应该在功能逻辑的粒度级别上编写。