GoMock Documentation

repository·main·Indexed 27 days ago

https://github.com/golang/mock

A mocking framework for the Go programming language that integrates with the built-in testing package. It includes the mockgen tool for generating mock implementations of interfaces.

Tokens
262
Snippets
1
Records
3
Agent score
44%

What's inside golang/mock

  1. Explore GoMock usage via sample files

    main

    The sample directory provides a non-trivial example of mocking interfaces. Use these files to understand the workflow:

    • user.go: Contains the source code and interfaces intended for mocking.
    • user_test.go: Demonstrates how to use mocks in actual tests, including creating mock objects and setting up expectations.
    • mock_user_test.go: The actual code generated by mockgen. You can find the go:generate command used to create this file in ../gomock/matchers.go.
  2. Run the GoMock sample tests

    main

    To see a complete demonstration of GoMock in action—including interface definition, mock generation, and expectation setting—you can run the provided sample tests using the following command:

    go test github.com/golang/mock/sample