episode0

package
v0.0.0-...-c6e3130 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 29, 2022 License: Apache-2.0 Imports: 4 Imported by: 0

README

Mocking in Go

Watch The Screencast

Go in 5 Minutes, episode 0.

This screencast focuses on mocking external dependencies so you can write fast, focused unit tests for your code.

Screencast video: https://goin5minutes.com/screencast/episode_0_writing_testable_code_and_fast_unit_tests_using_mocking/

Outline

  1. Execute your code in isolation
  2. Swap out dependencies with local implementation. Example: Redis client
  3. Other languages:
  • Reflection
  • Monkey patching
  • Magic
  1. The Go Way = simplicity. Use an interface

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNotFound = errors.New("not found")
)

Functions

func BusinessLogic

func BusinessLogic(h HashTable)

BusinessLogic is a sample function that does some business logic using h as the data storage system. We will test this function

Types

type HashTable

type HashTable interface {
	Get(key string) ([]byte, error)
	Set(key string, value []byte) error
}

HashTable is the interface for a simple hash table. It's designed in such a way that some libraries immediately adhere to it with no extra code. One such library is https://godoc.org/github.com/hoisie/redis

func NewInMemoryHashTable

func NewInMemoryHashTable() HashTable

type RedisClientWrapper

type RedisClientWrapper struct {
	Client *redis.Client
}

RedisClientWrapper is a wrapper for the Client type the the gopkg.in/redis.v3 package This type allows to redefine the Get and Set method using the parameter types and return types that match the Hashtable interface.

func (RedisClientWrapper) Get

func (i RedisClientWrapper) Get(key string) ([]byte, error)

Get method of the RedisClientWrapper type

func (RedisClientWrapper) Set

func (i RedisClientWrapper) Set(key string, val []byte) error

Set method of the RedisClientWrapper type

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL