Subscribe to the Ardan Labs Insider

You’ll get our FREE Video Series & special offers on upcoming training events along with notifications on our latest blog posts.

Included in your subscription
  • Access to our free video previews
  • Updates on our latest blog posts
  • Discounts on upcoming events

Valid email required.

Submit failed. Try again or message us directly at hello@ardanlabs.com.

Thank You for Subscribing

Check your email for confirmation.

Interfaces 101 : Testing with a Mock Interface Ep. 8

Author image

Ardan Labs

Introduction

In episode 7, Miki discussed design considerations to keep in mind while creating interfaces in Go with the first idea he proposed being that an interface should represent what we need from a type, and not what is stored on the type. To add some clarity to this thought, Miki explained how the io.Reader and io.Writer interfaces each require one method to be implemented although the underlying concrete type may store more information. The second idea he proposed was that interfaces should be small, with less than 6 methods, to ensure your code remains composable.

In this video, Miki will build an API client for a web service. The type will have a method called Health that:

  • Performs an HTTP request to a URL.
  • Returns an error depending on the response code received from the server.

Miki will then proceed by writing unit tests to verify if the client is working as intended. In lieu of a real server, Miki will create a new type that implements the http.RoundTripper interface and use it as the request’s transport during testing to manually specify the response code returned. Watch and learn how to mock server responses for unit tests.

Things you will learn in this video

  • Perform an HTTP request with the Go STD library.
  • Mock an HTTP response for unit testing.

Video

Go Training

We have taught Go to thousands of developers all around the world since 2014. There is no other company that has been doing it longer and our material has proven to help jump start developers 6 to 12 months ahead of their knowledge of Go. We know what knowledge developers need in order to be productive and efficient when writing software in Go.

Our classes are perfect for both experienced and beginning engineers. We start every class from the beginning and get very detailed about the internals, mechanics, specification, guidelines, best practices and design philosophies. We cover a lot about "if performance matters" with a focus on mechanical sympathy, data oriented design, decoupling and writing production software.

Capital One
Cisco
Visa
Teradata
Red Ventures

Interested in Ultimate Go Corporate Training and special pricing?

Let’s Talk Corporate Training!

Join Our Online
Education Program

Our courses have been designed from training over 30,000 engineers since 2013, and they go beyond just being a language course. Our goal is to challenge every student to think about what they are doing and why.