Fix go buildable source file problem (#1213)
This commit is contained in:
49
examples/helloworld/mock_helloworld/hw_mock_test.go
Normal file
49
examples/helloworld/mock_helloworld/hw_mock_test.go
Normal file
@ -0,0 +1,49 @@
|
||||
package mock_helloworld_test
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/golang/mock/gomock"
|
||||
"github.com/golang/protobuf/proto"
|
||||
"golang.org/x/net/context"
|
||||
helloworld "google.golang.org/grpc/examples/helloworld/helloworld"
|
||||
hwmock "google.golang.org/grpc/examples/helloworld/mock_helloworld"
|
||||
)
|
||||
|
||||
// rpcMsg implements the gomock.Matcher interface
|
||||
type rpcMsg struct {
|
||||
msg proto.Message
|
||||
}
|
||||
|
||||
func (r *rpcMsg) Matches(msg interface{}) bool {
|
||||
m, ok := msg.(proto.Message)
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
return proto.Equal(m, r.msg)
|
||||
}
|
||||
|
||||
func (r *rpcMsg) String() string {
|
||||
return fmt.Sprintf("is %s", r.msg)
|
||||
}
|
||||
|
||||
func TestSayHello(t *testing.T) {
|
||||
ctrl := gomock.NewController(t)
|
||||
defer ctrl.Finish()
|
||||
mockGreeterClient := hwmock.NewMockGreeterClient(ctrl)
|
||||
req := &helloworld.HelloRequest{Name: "unit_test"}
|
||||
mockGreeterClient.EXPECT().SayHello(
|
||||
gomock.Any(),
|
||||
&rpcMsg{msg: req},
|
||||
).Return(&helloworld.HelloReply{Message: "Mocked Interface"}, nil)
|
||||
testSayHello(t, mockGreeterClient)
|
||||
}
|
||||
|
||||
func testSayHello(t *testing.T, client helloworld.GreeterClient) {
|
||||
r, err := client.SayHello(context.Background(), &helloworld.HelloRequest{Name: "unit_test"})
|
||||
if err != nil || r.Message != "Mocked Interface" {
|
||||
t.Errorf("mocking failed")
|
||||
}
|
||||
t.Log("Reply : ", r.Message)
|
||||
}
|
Reference in New Issue
Block a user