Skip to content

Commit

Permalink
fix(mocks): add missing //go:generate for mocks (#2273)
Browse files Browse the repository at this point in the history
- Replace some mockery mocks with gomock mocks when gomock mocks already exist
- Generate `mock_*_test.go` package local gomock files where needed
- Add missing mockery `//go:generate` comments
  • Loading branch information
qdm12 committed Feb 18, 2022
1 parent d20b8c3 commit f4f7465
Show file tree
Hide file tree
Showing 15 changed files with 278 additions and 733 deletions.
24 changes: 15 additions & 9 deletions dot/core/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ import (
"path/filepath"
"testing"

coremocks "github.com/ChainSafe/gossamer/dot/core/mocks"
"github.com/ChainSafe/gossamer/dot/network"
"github.com/ChainSafe/gossamer/dot/peerset"
"github.com/ChainSafe/gossamer/dot/state"
"github.com/ChainSafe/gossamer/dot/types"
"github.com/ChainSafe/gossamer/internal/log"
"github.com/ChainSafe/gossamer/lib/common"
"github.com/ChainSafe/gossamer/lib/crypto/sr25519"
Expand All @@ -19,21 +21,23 @@ import (
"github.com/ChainSafe/gossamer/lib/runtime/wasmer"
"github.com/ChainSafe/gossamer/lib/utils"
"github.com/golang/mock/gomock"
"github.com/stretchr/testify/mock"
"github.com/libp2p/go-libp2p-core/peer"
"github.com/stretchr/testify/require"
)

// NewTestService creates a new test core service
func NewTestService(t *testing.T, cfg *Config) *Service {
t.Helper()
ctrl := gomock.NewController(t)

if cfg == nil {
cfg = &Config{}
}

if cfg.DigestHandler == nil {
cfg.DigestHandler = new(coremocks.DigestHandler)
cfg.DigestHandler.(*coremocks.DigestHandler).On("HandleDigests", mock.AnythingOfType("*types.Header"))
digestHandler := NewMockDigestHandler(ctrl)
digestHandler.EXPECT().HandleDigests(gomock.AssignableToTypeOf(new(types.Header)))
cfg.DigestHandler = digestHandler
}

if cfg.Keystore == nil {
Expand All @@ -56,7 +60,6 @@ func NewTestService(t *testing.T, cfg *Config) *Service {
if cfg.BlockState == nil || cfg.StorageState == nil ||
cfg.TransactionState == nil || cfg.EpochState == nil ||
cfg.CodeSubstitutedState == nil {
ctrl := gomock.NewController(t)
telemetryMock := NewMockClient(ctrl)
telemetryMock.EXPECT().SendMessage(gomock.Any()).AnyTimes()

Expand Down Expand Up @@ -123,10 +126,13 @@ func NewTestService(t *testing.T, cfg *Config) *Service {
cfg.BlockState.StoreRuntime(cfg.BlockState.BestBlockHash(), cfg.Runtime)

if cfg.Network == nil {
net := new(coremocks.Network)
net.On("GossipMessage", mock.AnythingOfType("*network.TransactionMessage"))
net.On("IsSynced").Return(true)
net.On("ReportPeer", mock.AnythingOfType("peerset.ReputationChange"), mock.AnythingOfType("peer.ID"))
net := NewMockNetwork(ctrl)
net.EXPECT().GossipMessage(gomock.AssignableToTypeOf(new(network.TransactionMessage)))
net.EXPECT().IsSynced().Return(true)
net.EXPECT().ReportPeer(
gomock.AssignableToTypeOf(peerset.ReputationChange{}),
gomock.AssignableToTypeOf(peer.ID("")),
)
cfg.Network = net
}

Expand Down
8 changes: 4 additions & 4 deletions dot/core/messages_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import (
"github.com/golang/mock/gomock"
"github.com/stretchr/testify/require"

"github.com/ChainSafe/gossamer/dot/core/mocks"
"github.com/ChainSafe/gossamer/dot/network"
"github.com/ChainSafe/gossamer/dot/state"
"github.com/ChainSafe/gossamer/dot/sync"
Expand Down Expand Up @@ -70,7 +69,9 @@ func createExtrinsic(t *testing.T, rt runtime.Instance, genHash common.Hash, non
}

func TestService_HandleBlockProduced(t *testing.T) {
net := new(mocks.Network)
ctrl := gomock.NewController(t)

net := NewMockNetwork(ctrl)
cfg := &Config{
Network: net,
Keystore: keystore.NewGlobalKeystore(),
Expand Down Expand Up @@ -105,7 +106,7 @@ func TestService_HandleBlockProduced(t *testing.T) {
BestBlock: true,
}

net.On("GossipMessage", expected)
net.EXPECT().GossipMessage(expected)

state, err := s.storageState.TrieState(nil)
require.NoError(t, err)
Expand All @@ -114,7 +115,6 @@ func TestService_HandleBlockProduced(t *testing.T) {
require.NoError(t, err)

time.Sleep(time.Second)
net.AssertCalled(t, "GossipMessage", expected)
}

func TestService_HandleTransactionMessage(t *testing.T) {
Expand Down
Loading

0 comments on commit f4f7465

Please sign in to comment.