Golang - Tests

0.0(0)
studied byStudied by 1 person
0.0(0)
call with kaiCall with Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/10

flashcard set

Earn XP

Description and Tags

Last updated 11:10 PM on 1/1/25
Name
Mastery
Learn
Test
Matching
Spaced
Call with Kai

No analytics yet

Send a link to your students to track their progress

11 Terms

1
New cards

What is the purpose of unit testing in Go?

To ensure that individual components of the code function correctly

2
New cards

Why might a developer choose Go's built-in mocking techniques over third-party solutions?

To gain a deeper understanding of the testing process and maintain closer control

3
New cards

What are higher-order functions in Go's mocking context?

Functions that take other functions as parameters or return them

4
New cards

What is monkey patching in Go?

A technique involving reassignment of package-level variables to mock implementations during testing, allowing control over function behavior without altering the original code. For example:

var fetchData = realFetchData  
fetchData = mockFetchData  

5
New cards

How does interface substitution aid in mocking within Go?

By defining interfaces that represent desired behaviors, allowing the replacement of concrete types with mock implementations that satisfy these interfaces during testing. For example:

type Fetcher interface { Fetch(id int) (string, error) }  
type MockFetcher struct {}  
func (m MockFetcher) Fetch(id int) (string, error) { return "mock data", nil }  

6
New cards

What is the purpose of embedding interfaces in Go's testing?

To extend existing interfaces by embedding them into new ones, facilitating the creation of mock implementations that include additional methods or behaviors. For example:

type BaseFetcher interface { Fetch(id int) (string, error) }  
type AdvancedFetcher interface { BaseFetcher; ExtraFetch(param string) bool }  

7
New cards

How can downstream HTTP calls be mocked in Go?

By using Go's net/http/httptest package to create mock servers that simulate HTTP responses. For example:

server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {  
    w.WriteHeader(http.StatusOK)  
    w.Write([]byte(`{"message":"mock"}`))  
}))  

8
New cards

What caution should be taken when using higher-order functions for mocking in Go?

They can make code harder to read and expand function parameter lists, potentially increasing dependencies for packages that call the function. For example, passing a mockFetch to multiple layers of function calls could make debugging harder.

9
New cards

What are potential drawbacks of monkey patching in Go tests?

It may hinder parallel test execution due to shared variable manipulation and could require making variables public, exposing them to unintended external modifications. For example, patching a fetchData variable can create conflicts if tests run concurrently.

10
New cards

Why is interface substitution considered powerful for mocking in Go?

Because Go's interfaces are implicitly and statically satisfied by implementing types, allowing for flexible and type-safe mock implementations without explicit declarations. For example, any type that has a Fetch(id int) (string, error) method satisfies a Fetcher interface.

11
New cards

What are the different types of mocking techniques in Go?

  1. Higher-order functions – Functions that take or return other functions. For example:

type FetchFunc func(id int) (string, error)
func MockFetch(id int) (string, error) { return "mock data", nil }
  1. Monkey patching – Reassigning package-level variables to mock implementations. For example:

var fetchData = realFetchData
fetchData = mockFetchData
  1. Interface substitution – Defining interfaces and replacing concrete types with mocks. For example:

type Fetcher interface { Fetch(id int) (string, error) }
type MockFetcher struct {}
func (m MockFetcher) Fetch(id int) (string, error) { return "mock data", nil }
  1. Embedding interfaces – Extending existing interfaces to create advanced mock implementations. For example:

type BaseFetcher interface { Fetch(id int) (string, error) }
type AdvancedFetcher interface { BaseFetcher; ExtraFetch(param string) bool }
  1. Mocking HTTP calls – Using httptest to simulate HTTP servers. For example:

server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    w.WriteHeader(http.StatusOK)
    w.Write([]byte(`{"message":"mock"}`))
}))