Last active
July 4, 2021 13:19
-
-
Save tauraamui/d288bd4dfb6d86d39f8a558625d0f41b to your computer and use it in GitHub Desktop.
Three different approaches to making it possible to cover logging outputs in code tests
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// VERSION 1 | |
err := c.cache.Set(sizeOnDiskCacheKey, []byte(sizeStrWithUnitSuffix)) | |
if err != nil { | |
// impossible to mock as logging.Error is unoverridable func def | |
logging.Error("unable to store disk size in cache: %w", err) | |
} | |
// VERSION 2 | |
// define local overridable log error wrapper | |
var logConnError = func(format string, args ...interface{}) { | |
logging.Error(format, args...) | |
} | |
err := c.cache.Set(sizeOnDiskCacheKey, []byte(sizeStrWithUnitSuffix)) | |
if err != nil { | |
logConnError("unable to store disk size in cache: %w", err) | |
} | |
// VERSION 3 | |
// define the same overridable log error wrapper but in a separate | |
// package called "log" which allows for similar calling symantics | |
err := c.cache.Set(sizeOnDiskCacheKey, []byte(sizeStrWithUnitSuffix)) | |
if err != nil { | |
// calling same wrapper as above except the | |
// wrapping func declared in separate package | |
log.Error("unable to store disk size in cache: %w", err) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment