Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletter Hub
Free Learning
Arrow right icon
timer SALE ENDS IN
0 Days
:
00 Hours
:
00 Minutes
:
00 Seconds
Arrow up icon
GO TO TOP
Go Cookbook

You're reading from   Go Cookbook Build modular, readable, and testable applications in Go

Arrow left icon
Product type Paperback
Published in Jun 2017
Publisher Packt
ISBN-13 9781783286836
Length 400 pages
Edition 1st Edition
Languages
Arrow right icon
Author (1):
Arrow left icon
 Torres Torres
Author Profile Icon Torres
Torres
Arrow right icon
View More author details
Toc

Table of Contents (20) Chapters Close

Title Page
Credits
About the Author
About the Reviewer
www.PacktPub.com
Customer Feedback
Preface
1. I/O and File Systems FREE CHAPTER 2. Command-Line Tools 3. Data Conversion and Composition 4. Error Handling in Go 5. All about Databases and Storage 6. Web Clients and APIs 7. Microservices for Applications in Go 8. Testing 9. Parallelism and Concurrency 10. Distributed Systems 11. Reactive Programming and Data Streams 12. Serverless Programming 13. Performance Improvements, Tips, and Tricks

Using the Mockgen package


The previous example used our custom mock objects. When you're working with a lot of interfaces, writing these can become cumbersome and error prone. This is a place where generating code makes a lot of sense. Fortunately, there's a package called github.com/golang/mock/gomock that provides a generation of mock objects and gives us a very useful library to use in conjunction with interface testing.

This recipe will explore some of the functionality of gomock and will cover trade-offs on where, when, and how to work with and generate mock objects.

Getting ready

Configure your environment according to these steps:

  1. Refer to the Getting ready section of theMocking using the standard library recipe of this chapter.
  2. Run the go get github.com/golang/mock/ command.

How to do it...

These steps cover writing and running your application:

  1. From your terminal/console application, create the chapter8/mockgen directory and navigate to it.
  2. Copy tests from https://github.com/agtorre/go-cookbook...
lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime
Visually different images