DEV Community

HarmonyOS
HarmonyOS

Posted on

How to Resolve the Issue of Base64Helper Inheritance Not Working in Mock

Read the original article:How to Resolve the Issue of Base64Helper Inheritance Not Working in Mock

Problem Description

The inheritance of Base64Helper in the mock does not take effect.
The code is as follows:

// Base64HelperMock.mock.ets
import { util } from '@kit.ArkTS'

export class Base64HelperMock extends util.Base64Helper {
  decodeSync(src: string | Uint8Array, options?: util.Type | undefined): Uint8Array {
    return new Uint8Array([99,97,10]);
  }
  encodeSync(src: Uint8Array, options?: util.Type | undefined) {
    return new Uint8Array([99,97,10]);
  }
  encodeToStringSync(src: Uint8Array, options?: util.Type | undefined): string {
    return '';
  }
}
// mock-config.json
{
"@ohos.util": {
    "source": "src/mock/Base64Helper.mock.ets"
  }
}
Enter fullscreen mode Exit fullscreen mode

The following error occurs running:

1.png

Background Knowledge

Mock capability: In actual development, some interfaces or objects depend on external or complex logic, making them difficult to reproduce in test environments. This makes these interfaces or objects hard to test. In such cases, mock capability can be used.

Troubleshooting Process

Perform the following operations:

  • Ensure that the export mode of the mock file is the same as that of the mocked interface.
  • To view the export mode of the mocked interface, press Ctrl and click the mocked interface, as shown in the following figure.

2.png

  • View the export mode of the mock file, as shown in the following figure.

3.png

Analysis Conclusion

The export method of the mock file is different from that of the mocked API.

Solution

The export mode of the mock file must be the same as that of the mock (util) API. The export mode of the util API is export default util. Therefore, the export mode of the mock file must be export default mockUtil.

Example code:

1. Create a new ArkTS file under the src/mockBase64HelperMock.mock.ets, and define the mock implementation of the target module in this file.

import { util } from '@kit.ArkTS'
type MockUtil = Record<string, Object>;

export class Base64HelperMock {
  decodeSync(src: string | Uint8Array, options?: util.Type | undefined): Uint8Array {
    console.info('run mock')
    return new Uint8Array([99,97,10]);
  }
  encodeSync(src: Uint8Array, options?: util.Type | undefined) {
    return new Uint8Array([99,97,10]);
  }
  encodeToStringSync(src: Uint8Array, options?: util.Type | undefined): string {
    return '';
  }
}

const mockUtil: MockUtil = {
  'Base64Helper': Base64HelperMock,
}

export default mockUtil
Enter fullscreen mode Exit fullscreen mode

2. Define the mapping relationship between the target module and the mock implementation in the mock configuration file src/mock/mock-config.json5.

{
  "@ohos.util": {
    "source": "src/mock/Base64HelperMock.mock.ets"
  }
}
Enter fullscreen mode Exit fullscreen mode

3. Write the following code in the test file.

import { util } from '@kit.ArkTS'
import { describe, it } from '@ohos/hypium';

export default function localUnitTest() {
  describe('localUnitTest', () => {
    it('assertContain', 0, () => {
      const array = new util.Base64Helper().decodeSync('')
    });
  });
}
Enter fullscreen mode Exit fullscreen mode

Verification Result

4.png

Related Documents or Links

Mock Capability Documentation

Written by Can Kankaya

Top comments (0)