Skip to content

Commit 6c721ba

Browse files
committed
fix(mocks): to have a corresponding //go:generate
- Replace some mockery mocks with gomock mocks when gomock mocks already exist - Generate _test package local gomock files where needed - Add missing mockery `//go:generate` comments
1 parent fbd13d2 commit 6c721ba

15 files changed

+278
-733
lines changed

dot/core/helpers_test.go

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@ import (
77
"path/filepath"
88
"testing"
99

10-
coremocks "github.com/ChainSafe/gossamer/dot/core/mocks"
10+
"github.com/ChainSafe/gossamer/dot/network"
11+
"github.com/ChainSafe/gossamer/dot/peerset"
1112
"github.com/ChainSafe/gossamer/dot/state"
13+
"github.com/ChainSafe/gossamer/dot/types"
1214
"github.com/ChainSafe/gossamer/internal/log"
1315
"github.com/ChainSafe/gossamer/lib/common"
1416
"github.com/ChainSafe/gossamer/lib/crypto/sr25519"
@@ -19,21 +21,23 @@ import (
1921
"github.com/ChainSafe/gossamer/lib/runtime/wasmer"
2022
"github.com/ChainSafe/gossamer/lib/utils"
2123
"github.com/golang/mock/gomock"
22-
"github.com/stretchr/testify/mock"
24+
"github.com/libp2p/go-libp2p-core/peer"
2325
"github.com/stretchr/testify/require"
2426
)
2527

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

3033
if cfg == nil {
3134
cfg = &Config{}
3235
}
3336

3437
if cfg.DigestHandler == nil {
35-
cfg.DigestHandler = new(coremocks.DigestHandler)
36-
cfg.DigestHandler.(*coremocks.DigestHandler).On("HandleDigests", mock.AnythingOfType("*types.Header"))
38+
digestHandler := NewMockDigestHandler(ctrl)
39+
digestHandler.EXPECT().HandleDigests(gomock.AssignableToTypeOf(new(types.Header)))
40+
cfg.DigestHandler = digestHandler
3741
}
3842

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

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

125128
if cfg.Network == nil {
126-
net := new(coremocks.Network)
127-
net.On("GossipMessage", mock.AnythingOfType("*network.TransactionMessage"))
128-
net.On("IsSynced").Return(true)
129-
net.On("ReportPeer", mock.AnythingOfType("peerset.ReputationChange"), mock.AnythingOfType("peer.ID"))
129+
net := NewMockNetwork(ctrl)
130+
net.EXPECT().GossipMessage(gomock.AssignableToTypeOf(new(network.TransactionMessage)))
131+
net.EXPECT().IsSynced().Return(true)
132+
net.EXPECT().ReportPeer(
133+
gomock.AssignableToTypeOf(peerset.ReputationChange{}),
134+
gomock.AssignableToTypeOf(peer.ID("")),
135+
)
130136
cfg.Network = net
131137
}
132138

dot/core/messages_integration_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import (
1616
"github.com/golang/mock/gomock"
1717
"github.com/stretchr/testify/require"
1818

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

7271
func TestService_HandleBlockProduced(t *testing.T) {
73-
net := new(mocks.Network)
72+
ctrl := gomock.NewController(t)
73+
74+
net := NewMockNetwork(ctrl)
7475
cfg := &Config{
7576
Network: net,
7677
Keystore: keystore.NewGlobalKeystore(),
@@ -105,7 +106,7 @@ func TestService_HandleBlockProduced(t *testing.T) {
105106
BestBlock: true,
106107
}
107108

108-
net.On("GossipMessage", expected)
109+
net.EXPECT().GossipMessage(expected)
109110

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

116117
time.Sleep(time.Second)
117-
net.AssertCalled(t, "GossipMessage", expected)
118118
}
119119

120120
func TestService_HandleTransactionMessage(t *testing.T) {

0 commit comments

Comments
 (0)