Skip to content

Instantly share code, notes, and snippets.

@madflojo
Created February 14, 2021 19:02
Show Gist options
  • Save madflojo/2465d376a1a94cccf225e0faf8b43308 to your computer and use it in GitHub Desktop.
Save madflojo/2465d376a1a94cccf225e0faf8b43308 to your computer and use it in GitHub Desktop.
Interface Article - MockDB Tests
func TestIsOver9k(t *testing.T) {
t.Run("TestOver9000", func(t *testing.T) {
DB = &MockDB{
FakeFetch: func(string) (int, error) {
return 9001, nil
},
}
if !isOver9000() {
t.Errorf("Test did not return expected results")
}
})
t.Run("TestUnder9000", func(t *testing.T) {
DB = &MockDB{
FakeFetch: func(string) (int, error) {
return 8999, nil
},
}
if isOver9000() {
t.Errorf("Test did not return expected results")
}
})
t.Run("TestDBError", func(t *testing.T) {
DB = &MockDB{
FakeFetch: func(string) (int, error) {
return 0, fmt.Errorf("unable to connect to database")
},
}
if isOver9000() {
t.Errorf("Test did not return expected results")
}
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment