Skip to content

mockery ¶

Mockery is a project that creates mock implementations of Golang interfaces. The mocks generated in this project are based off of the github.com/stretchr/testify suite of testing packages.

Why mockery? ¶

When you have an interface like this:

db.go
 type DB interface {
     Get(val string) string
 }

and a function that takes this interface:

db_getter.go
 func getFromDB(db DB) string {
     return db.Get("ice cream")
 }

You can test getFromDB by either instantiating a testing database, or you can simply create a mock implementation of DB using mockery. Mockery can automatically generate a mock implementation that allows us to define assertions on how the mock was used, what to return, and other useful tidbits. We can add a //go:generate directive above our interface:

db.go
 //go:generate mockery --name DB
 type DB interface {
     Get(val string) string
 }
.mockery.yaml
 inpackage: True # (1)!
 with-expecter: True # (2)!
 testonly: True # (3)!
  1. Generate our mocks next to the original interface
  2. Create expecter methods
  3. Append _test.go to the filename so the mock object is not packaged
Bash
 $ go generate  
 05 Mar 23 21:49 CST INF Starting mockery dry-run=false version=v2.20.0
 05 Mar 23 21:49 CST INF Using config: .mockery.yaml dry-run=false version=v2.20.0
 05 Mar 23 21:49 CST INF Walking dry-run=false version=v2.20.0
 05 Mar 23 21:49 CST INF Generating mock dry-run=false interface=DB qualified-name=github.com/vektra/mockery/v2/pkg/fixtures/example_project version=v2.20.0

We can then use the mock object in a test:

db_getter_test.go
 import (
     "testing"
 
     "github.com/stretchr/testify/assert"
 )
 
 func Test_getFromDB(t *testing.T) {
     mockDB := NewMockDB(t)
     mockDB.EXPECT().Get("ice cream").Return("chocolate").Once()
     flavor := getFromDB(mockDB)
     assert.Equal(t, "chocolate", flavor)
 }

Why use mockery over gomock? ¶

  1. mockery provides a much more user-friendly API and is less confusing to use
  2. mockery utilizes testify which is a robust and highly feature-rich testing framework
  3. mockery has rich configuration options that allow fine-grained control over how your mocks are generated
  4. mockery's CLI is more robust, user-friendly, and provides many more options
  5. mockery supports generics (this may no longer be an advantage if/when gomock supports generics)

Who uses mockery? ¶

  • Grafana logo
    Grafana
  • Google logo
    Google Skia
  • Google logo
    Google Skyzkaller
  • Hashicorp logo
    Hashicorp
  • Uber logo
    Uber Cadence
  • Jaeger logo
    Jaegertracing
  • Splunk logo
    Splunk kafka-mq-go
  • Ignite Logo
  • Tendermint Logo
  • Datadog logo
  • Seatgeek Logo
  • Amazon logo
    eksctl
  • MongoDB Logo
  • go-task logo
    Task
  • cerbos logo

Get Started

玻璃钢生产厂家寻求优质的玻璃钢花盆合肥商场中秋美陈深圳玻璃钢花盆梅州led发光玻璃钢雕塑工艺玻璃钢雕塑包运费吗玻璃钢花盆高端大气湛江玻璃钢龙雕塑大象玻璃钢卡通雕塑订做置办玻璃钢动物雕塑玻璃钢雕塑景观小品甘南动物玻璃钢雕塑哪家好护栏玻璃钢花盆价格重庆人物玻璃钢雕塑优势福建玻璃钢雕塑多少钱南充玻璃钢卡通雕塑价格甘肃基督教玻璃钢雕塑宁安玻璃钢仿铜雕塑四川特色商场美陈批发九江商场美陈商场美陈机器人吴忠植物玻璃钢雕塑厂家深圳贴马赛克玻璃钢雕塑玻璃钢雕塑怎么算价格斑马玻璃钢雕塑宜兴商场美陈布置商场主题创意美陈河南太湖石玻璃钢卡通雕塑价格四川玻璃钢桃子雕塑商场玻璃钢雕塑规格临沂玻璃钢关公雕塑香港通过《维护国家安全条例》两大学生合买彩票中奖一人不认账让美丽中国“从细节出发”19岁小伙救下5人后溺亡 多方发声单亲妈妈陷入热恋 14岁儿子报警汪小菲曝离婚始末遭遇山火的松茸之乡雅江山火三名扑火人员牺牲系谣言何赛飞追着代拍打萧美琴窜访捷克 外交部回应卫健委通报少年有偿捐血浆16次猝死手机成瘾是影响睡眠质量重要因素高校汽车撞人致3死16伤 司机系学生315晚会后胖东来又人满为患了小米汽车超级工厂正式揭幕中国拥有亿元资产的家庭达13.3万户周杰伦一审败诉网易男孩8年未见母亲被告知被遗忘许家印被限制高消费饲养员用铁锨驱打大熊猫被辞退男子被猫抓伤后确诊“猫抓病”特朗普无法缴纳4.54亿美元罚金倪萍分享减重40斤方法联合利华开始重组张家界的山上“长”满了韩国人?张立群任西安交通大学校长杨倩无缘巴黎奥运“重生之我在北大当嫡校长”黑马情侣提车了专访95后高颜值猪保姆考生莫言也上北大硕士复试名单了网友洛杉矶偶遇贾玲专家建议不必谈骨泥色变沉迷短剧的人就像掉进了杀猪盘奥巴马现身唐宁街 黑色着装引猜测七年后宇文玥被薅头发捞上岸事业单位女子向同事水杯投不明物质凯特王妃现身!外出购物视频曝光河南驻马店通报西平中学跳楼事件王树国卸任西安交大校长 师生送别恒大被罚41.75亿到底怎么缴男子被流浪猫绊倒 投喂者赔24万房客欠租失踪 房东直发愁西双版纳热带植物园回应蜉蝣大爆发钱人豪晒法院裁定实锤抄袭外国人感慨凌晨的中国很安全胖东来员工每周单休无小长假白宫:哈马斯三号人物被杀测试车高速逃费 小米:已补缴老人退休金被冒领16年 金额超20万

玻璃钢生产厂家 XML地图 TXT地图 虚拟主机 SEO 网站制作 网站优化