Thursday, May 2, 2024
HomeGolangTesting with a Mock Interface Ep. 8

Testing with a Mock Interface Ep. 8


Introduction

In episode 7, Miki mentioned design issues to bear in mind whereas creating interfaces in Go together with the primary thought he proposed being that an interface ought to characterize what we’d like from a kind, and never what’s saved on the sort. So as to add some readability to this thought, Miki defined how the io.Reader and io.Author interfaces every require one methodology to be carried out though the underlying concrete sort might retailer extra data. The second thought he proposed was that interfaces must be small, with lower than 6 strategies, to make sure your code stays composable.

On this video, Miki will construct an API consumer for an internet service. The kind can have a way known as Well being that:

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

Miki will then proceed by writing unit checks to confirm if the consumer is working as meant. In lieu of an actual server, Miki will create a brand new sort that implements the http.RoundTripper interface and use it because the request’s transport throughout testing to manually specify the response code returned. Watch and learn to mock server responses for unit checks.

Issues you’ll be taught on this video

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

Video



RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments