
Security News
Meet Socket at Black Hat Europe and BSides London 2025
Socket is heading to London! Stop by our booth or schedule a meeting to see what we've been working on.
A library that provides functionality for mocking and patching any arbitrary functions or methods in Go. It uses bouke/moneky to patch the actual function and provides gomock like features.
The NewPatch function creates a new Mock instance and returns a pointer to it. It is a convenient way to initialize a Mock and immediately call the Patch method.
The Patch method takes a target function as an argument and patches it with the generated replacement function. It assigns the patched function to the patchedFunc field and uses the Patch function from an external package to perform the actual patching.
The PatchInstance method is similar to Patch but is used for patching methods on specific instances of a type. It takes a target instance and the name of the method to be patched.
The AnyTimes method sets the anyTimes flag to indicate that the mocked function can be called any number of times.
The Times method sets the times field to specify the expected number of times the mocked function should be called.
The Return method allows specifying the return values for each invocation of the mocked function. It takes variadic arguments and converts them to reflect.Value before appending them to the returnValues slice.
The DoAndReturn method takes a function as an argument and assigns it to the returnFn field. This function will be called instead of returning predefined values when the mocked function is invoked.
The Unpatch method unpatches the original function, restoring it to its initial state.
func add(a int64, b int64) int64 {
return a + b
}
func sum(a, b int64) int64 {
return add(a, b)
}
func TestMocker(tt *testing.T) {
mock := Mock{}
mock.Patch(add).Times(1).Return(int64(0))
mock.Patch(add).Times(1).Return(int64(1))
mock.Patch(add).Times(1).Return(int64(2))
log.Printf("%v", sum(1, 2))
log.Printf("%v", sum(1, 2))
log.Printf("%v", sum(1, 2))
mock.Unpatch()
}
FAQs
Unknown package
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Socket is heading to London! Stop by our booth or schedule a meeting to see what we've been working on.

Security News
OWASP’s 2025 Top 10 introduces Software Supply Chain Failures as a new category, reflecting rising concern over dependency and build system risks.

Research
/Security News
Socket researchers discovered nine malicious NuGet packages that use time-delayed payloads to crash applications and corrupt industrial control systems.