{"repo":"matryer/moq","free":true,"listed":false,"github":"https://github.com/matryer/moq","clone":"git clone https://github.com/matryer/moq.git","description":"Interface mocking tool for go generate","language":"Go","stars":2206,"topics":["golang","mocking","testing","codegen"],"license":"MIT","category":"dev-tools","readme_excerpt":"Interface mocking tool for go generate. What is Moq? Moq is a tool that generates a struct from any interface. The struct can be used in test code as a mock of the interface. above: Moq generates the code on the right. You can read more in the Meet Moq blog post. Installing To start using latest released version of Moq, just run: Note that Go 1.18+ is needed for installing from source. For using Moq with older Go versions, use the pre-built binaries published with Moq releases. Usage NOTE: source-dir is the directory where the source code (definition) of the target interface is located. It needs to be a path to a directory and not the import statement for a Go package. In a command line: In code (for go generate): Then run go generate for your package. How to use it Mocking interfaces is a nice way to write unit tests where you can easily control the behaviour of the mocked object. Moq creates a struct that has a function field for each method, which you can declare in your test code. In this example, Moq generated the EmailSenderMock type: The mocked structure implements the interface, where each method calls the associated function field. Tips Keep mocked logic inside the test that is using it Only mock the fields you need It will panic if a nil function gets called Name arguments in the interface for a better experience Use closured variables inside your test function to capture details about the calls to the methods Use .MethodCalls() to track the calls Use .ResetCalls() ","default_branch":null,"files":null,"tree":[],"storefront":"/r/matryer","claimed":false,"request_supported":{"post":"https://gitbuyer.com/r/matryer/moq/request-supported","requests":0},"note":"indexed from public GitHub; nothing is for sale on this page. Clone it from GitHub. Paid listings live at /search."}