funcTestFoo(t*testing.T) {
ctrl:=gomock.NewController(t)
deferctrl.Finish()
m:=NewMockFoo(ctrl)
// Does not make any assertions. Executes the anonymous functions and returns// its result when Bar is invoked with 99.m.
EXPECT().
Bar(gomock.Eq(99)).
DoAndReturn(func(_int) int {
time.Sleep(1*time.Second)
return101
}).
AnyTimes()
// Does not make any assertions. Returns 103 when Bar is invoked with 101.m.
EXPECT().
Bar(gomock.Eq(101)).
Return(103).
AnyTimes()
SUT(m)
}
Integration tests
use any contributed official driver for database to connect to real DEV db
package main
import (
"net""testing""github.com/hashicorp/vault/api""github.com/hashicorp/vault/http""github.com/hashicorp/vault/vault"
)
funcTestVaultStuff(t*testing.T) {
ln, client:=createTestVault(t)
deferln.Close()
// Pass the client to the code under test.myFunction(client)
}
funccreateTestVault(t*testing.T) (net.Listener, *api.Client) {
t.Helper()
// Create an in-memory, unsealed core (the "backend", if you will).core, keyShares, rootToken:=vault.TestCoreUnsealed(t)
_=keyShares// Start an HTTP server for the core.ln, addr:=http.TestServer(t, core)
// Create a client that talks to the server, initially authenticating with// the root token.conf:=api.DefaultConfig()
conf.Address=addrclient, err:=api.NewClient(conf)
iferr!=nil {
t.Fatal(err)
}
client.SetToken(rootToken)
// Setup required secrets, policies, etc._, err=client.Logical().Write("secret/foo", map[string]interface{}{
"secret": "bar",
})
iferr!=nil {
t.Fatal(err)
}
returnln, client
}