A tool that integrates SQL, HTTP,interface,Redis mock

Mockit

Go Report Card Go

目标:将mock变得简单,让代码维护变得容易

分支介绍

  • main 主分支,覆盖了单元测试
  • light 轻分支,去除了单元测试,简化了依赖项,方便其他团队使用

常见Mock难点

  • 不同中间件,mock库设计模式不一致,学习代价高,差异化明显
  • mock方案强依赖服务端,无法灵活解耦
  • 单元测试适配各种中间件的方案后,依赖管理杂乱
  • 综上所述不想写mock,也没有良好的可执行方案,放弃单测

mockit做到了什么

  • 统一简化语法
  • 无需服务端
  • 解耦依赖项
  • testMain统一管理

mockit 使用

目前支持

  • Redis,MySQL,Interface,HTTP
  • GRPC 可以使用proto生成interface使用Interface模拟
  • ElasticSearch
    • 使用HTTP方式代理client,不过es的返回值比较复杂,请求路径没有普通HTTP直观
    • 使用Interface方式,将dao层抽象成接口方式,这种方式下,接口返回值模拟相对方便直观

理论上业务抽象使用Interface方式都可达成

准备 (具体参照kit_test.go)

  1. interface生成 https://github.com/golang/mock
// interface生成方式
$ mockgen -source ./iface/interface.go -package mockimpl -destination ./mockimpl/interface.go
// 而后将以下new方法的返回值改成interface{}
before: func NewMockDemoInterface(ctrl *gomock.Controller) *MockDemoInterface {
after: func NewMockDemoInterface(ctrl *gomock.Controller) interface{}} {
    mock := &MockDemoInterface{ctrl: ctrl}
    mock.recorder = &MockDemoInterfaceMockRecorder{mock}
    return mock
}
  1. sqlmock依赖replace

目前需要替换下sqlmock库,目前pr还在合并中,预计最近2周就能OK

replace github.com/DATA-DOG/go-sqlmock v1.5.0 => github.com/Rennbon/go-sqlmock v1.5.1-0.20211212104631-9c4a20760689

mockit自身单测

  • 当前目录下 iface 中有4个方法, mockimpl中分别为各个mock实例的实现
  • kit_test.go中mockSrv引用了这些mockimpl实例,在testMain中列举了mock的启动方式,并且在之后所有的Test中介绍了如何使用

引用传送

Owner
SHIHUO
SHIHUO Open Source Task Force
SHIHUO
Similar Resources

Powerful mock generation tool for Go programming language

Summary Minimock generates mocks out of Go interface declarations. The main features of minimock are: It generates statically typed mocks and helpers.

Dec 17, 2022

Merge Mock - testing tool for the Ethereum Merge

MergeMock Experimental debug tooling, mocking the execution engine and consensus node for testing. work in progress Quick Start To get started, build

Oct 21, 2022

ESME is a go library that allows you to mock a RESTful service by defining the configuration in json format

ESME is a go library that allows you to mock a RESTful service by defining the configuration in json format. This service can then simply be consumed by any client to get the expected response.

Mar 2, 2021

mockery - A mock code autogenerator for Golang

mockery - A mock code autogenerator for Golang

mockery - A mock code autogenerator for Golang

Jan 8, 2023

Create your own mock server with just a JSON file!

Gmocker Run a blazing fast mock server in just seconds! 🚀 All you need is to make a json file that contains path and response mapping. See an example

Dec 21, 2022

Create your own blazing fast mock server with just a JSON file!

Gmocker Run a blazing fast mock server in just seconds! 🚀 All you need is to make a json file that contains path and response mapping. See an example

Dec 21, 2022

Apple Push Notification service (APNs) mock server

APNs Mock Server Server

Dec 8, 2022

Completely type-safe compile-time mock generator for Go

Mockc Mockc is a completely type-safe compile-time mock generator for Go. You can use it just by writing the mock generators with mockc.Implement() or

Aug 25, 2022

A mock of Go's net package for unit/integration testing

netmock: Simulate Go network connections netmock is a Go package for simulating net connections, including delays and disconnects. This is work in pro

Oct 27, 2021
CLI tool to mock TCP connections. You can use it with Detox, Cypress or any other framework to automatically mock your backend or database.

Falso It is a CLI that allows you to mock requests/responses between you and any server without any configuration or previous knowledge about how it w

Sep 23, 2022
Vault mock - Mock of Hashicorp Vault used for unit testing

vault_mock Mock of Hashicorp Vault used for unit testing Notice This is a person

Jan 19, 2022
Mock-the-fck - Mock exercise for human

Mock the fck Originally, Mockery-example Example case for mockery issue #128 fil

Jan 21, 2022
HTTP mock for Golang: record and replay HTTP/HTTPS interactions for offline testing

govcr A Word Of Warning I'm in the process of partly rewriting govcr to offer better support for cassette mutations. This is necessary because when I

Dec 28, 2022
Sql mock driver for golang to test database interactions

Sql driver mock for Golang sqlmock is a mock library implementing sql/driver. Which has one and only purpose - to simulate any sql driver behavior in

Dec 31, 2022
Go testing in the browser. Integrates with `go test`. Write behavioral tests in Go.
Go testing in the browser. Integrates with `go test`. Write behavioral tests in Go.

GoConvey is awesome Go testing Welcome to GoConvey, a yummy Go testing tool for gophers. Works with go test. Use it in the terminal or browser accordi

Dec 30, 2022
Go testing in the browser. Integrates with `go test`. Write behavioral tests in Go.
Go testing in the browser. Integrates with `go test`. Write behavioral tests in Go.

GoConvey is awesome Go testing Welcome to GoConvey, a yummy Go testing tool for gophers. Works with go test. Use it in the terminal or browser accordi

Dec 30, 2022
Mock object for Go http.ResponseWriter

mockhttp -- Go package for unit testing HTTP serving Unit testing HTTP services written in Go means you need to call their ServeHTTP receiver. For thi

Sep 27, 2022
A basic lightweight HTTP client for Go with included mock features.

A basic lightweight HTTP client for Go with included mock features. Features Support almost all http method like G

May 2, 2022
📡 mock is a simple, cross-platform, cli app to simulate HTTP-based APIs.
 📡 mock is a simple, cross-platform, cli app to simulate HTTP-based APIs.

mock ?? mock is a simple, cross-platform, cli app to simulate HTTP-based APIs. About mock Mock allows you to spin up a local http server based of a .m

May 6, 2022