-
Notifications
You must be signed in to change notification settings - Fork 0
/
service_test.go
63 lines (51 loc) · 1.59 KB
/
service_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package main
import (
"testing"
"github.com/alr-lab/test-double-go/service"
)
const email = "fake"
// MockStore describes a mocked datastore, a datastore which is doing some
// logic but also implements tools to validate expectations after the test
// execution.
type MockStore struct {
methods map[string]bool
}
func NewStore() *MockStore {
store := &MockStore{methods: make(map[string]bool)}
return store
}
// ExpectToCall method adds a method to the list of expected methods to be
// called so that we can validate them after the test execution.
func (s *MockStore) ExpectToCall(method string) {
s.methods[method] = false
}
// Verify method allows us to validate that methods were called.
func (s *MockStore) Verify(t *testing.T) {
for method, called := range s.methods {
if !called {
t.Fatalf("method %q not called", method)
}
}
}
// GetCustomerEmail returns a customer email.
//
// The mocked function allows us to make sure the system under test would
// have called the production-ready function, as a spy would do. But we
// could go one step further and actually count the number of times the
// method was called, and expect the function to be called a very specific
// number of times. We could also implement some very specific scenarios as
// fake doubles.
func (s *MockStore) GetCustomerEmail(id int) string {
s.methods["GetCustomerEmail"] = true
return email
}
func TestService_Get(t *testing.T) {
store := NewStore()
serv := service.New(store)
store.ExpectToCall("GetCustomerEmail")
got := serv.Get()
if got != email {
t.Fatalf("got %q, want %q", got, email)
}
store.Verify(t)
}