Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Mock dependency implementations for blog post example. Copy and paste from the blog post to complete
package main
import (
"net/http"
"fmt"
"errors"
"context"
)
// mock implementations
func NewPooledDatabaseConnection(url string) Database {
return mockDatabase{}
}
func NewDatabaseUserManager(databaseConn Database) UserManager {
return mockUserManager{}
}
type mockDatabase struct {}
type mockUserManager struct {}
func (um mockUserManager) GetUser(id int) (user User, e error) {
if id == 1 {
user = mockUser{id: 1, name: "John Doe"}
} else {
e = errors.New("User not found.")
}
return
}
func (um mockUserManager) AuthenticateUser(username, password string) (id int, e error) {
if username == "john.d" && password == "1234" {
id = 1
} else {
e = errors.New("Invalid username or password.")
}
return
}
type mockUser struct {
id int
name string
}
func (u mockUser) Id() int {
return u.id
}
func (u mockUser) Name() string {
return u.name
}
// dependency interfaces
// authentication middleware
// http handler
// main function
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment