testing-knowledge

Provides Mockey mocking guidance for writing Golang unit tests.

Updated Apr 30, 2026
One-click install
npx skills add https://github.com/yuezhen-huang/my-bytedance-skillhub --skill testing-knowledge-yuezhen-huang
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: testing-knowledge
Source: https://github.com/yuezhen-huang/my-bytedance-skillhub/tree/main/testing-knowledge
Command: npx skills add https://github.com/yuezhen-huang/my-bytedance-skillhub --skill testing-knowledge-yuezhen-huang

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve? Writing Golang unit tests often requires mocking functions, methods, and external dependencies, and developers need clear guidance on using the Mockey framework correctly, including its compiler flag requirements and common pitfalls. ## Core Features & Use Cases - Function and Method Mocking: Mock plain functions, struct methods with value or pointer receivers, and generic functions via MockGeneric. - Advanced Mock Behaviors: Conditional mocks with When, sequential returns with Sequence, decorator patterns, and goroutine-scoped filtering. - Scoped Test Management: Use PatchConvey with goconvey to automatically release mocks after each test case. - Use Case: When writing a unit test for a service that calls an RPC client, mock the RPC function with Mockey, disable inlining via -gcflags="all=-l -N", and assert results inside PatchConvey blocks. ## Quick Start Show me how to mock a function in my Go unit test using Mockey with PatchConvey.

Frequently Asked Questions about testing-knowledge

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I mock a function in Golang unit tests with Mockey?

Call Mock(targetFunc).Return(value).Build() to replace a function's behavior, typically inside a PatchConvey block so the mock is released automatically after the test. Remember to run tests with -gcflags="all=-l -N" to disable inlining.

How to mock generic functions or methods in Go?

Use MockGeneric instead of Mock for generic functions and methods, specifying the instantiated type such as MockGeneric(FooGeneric[string]).Return(value).Build(). The standard Mock API does not handle generics.

Why is my Mockey mock not working in Go tests?

The most common cause is not disabling inlining and compiler optimizations; run tests with -gcflags="all=-l -N". Also verify you called .Build(), matched pointer versus value receivers correctly, and used MockGeneric for generics.

Does Mockey work with goconvey for test organization?

Yes, Mockey is designed to work with goconvey via PatchConvey, which scopes mocks so they are automatically released when the block exits. This prevents mock leakage between test cases.

What does the 'function is too short to patch' error mean in Mockey?

This error occurs when inlining is not disabled, the target function is too short to patch, or another tool already mocked it. Disable optimizations with -gcflags flags or use MockUnsafe for very short functions.

Which Go versions and platforms does Mockey support?

Mockey supports Go 1.13 and later on Mac OS, Linux, and Windows, across AMD64 and ARM64 architectures. It is widely used across ByteDance repositories for unit testing.