Skip to content

Instantly share code, notes, and snippets.

@squirly
Last active July 15, 2016 18:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save squirly/ca101d0dbe31ef77dfe0a350f981a19e to your computer and use it in GitHub Desktop.
Save squirly/ca101d0dbe31ef77dfe0a350f981a19e to your computer and use it in GitHub Desktop.
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