mirror of
https://github.com/openharmony/arkXtest.git
synced 2026-07-20 04:24:07 -04:00
!265 新增mockObj能力,解决编译器检查问题,无参数函数的支持
Merge pull request !265 from 壹米阳光/master
This commit is contained in:
Vendored
+2
-1
@@ -89,7 +89,7 @@ declare interface when {
|
||||
afterReturnNothing(): undefined
|
||||
afterAction(action: any): any
|
||||
afterThrow(e_msg: string): string
|
||||
(argMatchers: any): when;
|
||||
(argMatchers?: any): when;
|
||||
}
|
||||
|
||||
export interface VerificationMode {
|
||||
@@ -103,6 +103,7 @@ export interface VerificationMode {
|
||||
export class MockKit {
|
||||
constructor()
|
||||
mockFunc(obj: Object, func: Function): Function
|
||||
mockObject(obj: Object): Object
|
||||
verify(methodName: String, argsArray: Array<any>): VerificationMode
|
||||
ignoreMock(obj: Object, func: Function): void
|
||||
clear(obj: Object): void
|
||||
|
||||
@@ -94,6 +94,9 @@ class MockKit {
|
||||
values = new Map();
|
||||
}
|
||||
let key = params[0];
|
||||
if (typeof key == "undefined") {
|
||||
key = "anonymous-mock-" + f.propName;
|
||||
}
|
||||
let matcher = new ArgumentMatchers();
|
||||
if (matcher.matcheStubKey(key)) {
|
||||
key = matcher.matcheStubKey(key);
|
||||
@@ -111,6 +114,9 @@ class MockKit {
|
||||
return undefined;
|
||||
}
|
||||
let retrunKet = params[0];
|
||||
if (typeof retrunKet == "undefined") {
|
||||
retrunKet = "anonymous-mock-" + f.propName;
|
||||
}
|
||||
let stubSetKey = this.currentSetKey;
|
||||
|
||||
if (this.currentSetKey && (typeof (retrunKet) != "undefined")) {
|
||||
@@ -213,6 +219,21 @@ class MockKit {
|
||||
let a = this.recordCalls.get(methodName + '(' + argsArray.toString() + ')');
|
||||
return new VerificationMode(a ? a : 0);
|
||||
}
|
||||
|
||||
mockObject(object) {
|
||||
if (!object || typeof object === "string") {
|
||||
throw Error(`this ${object} cannot be mocked`);
|
||||
}
|
||||
const _this = this;
|
||||
let mockedObject = {};
|
||||
let keys = Reflect.ownKeys(object);
|
||||
keys.filter(key => (typeof Reflect.get(object, key)) === 'function')
|
||||
.forEach(key => {
|
||||
mockedObject[key] = object[key];
|
||||
mockedObject[key] = _this.mockFunc(mockedObject, mockedObject[key]);
|
||||
});
|
||||
return mockedObject;
|
||||
}
|
||||
}
|
||||
|
||||
function ifMockedFunction(f) {
|
||||
|
||||
Reference in New Issue
Block a user