diff --git a/Documentation/gomock-example.md b/Documentation/gomock-example.md index 9f7e023d..fa634a79 100644 --- a/Documentation/gomock-example.md +++ b/Documentation/gomock-example.md @@ -1,10 +1,10 @@ # Mocking Service for gRPC -[Example code](https://github.com/grpc/grpc-go/tree/master/examples/helloworld/mock) +[Example code](https://github.com/grpc/grpc-go/tree/master/examples/helloworld/mock_helloworld) ## Why? -To test client-side logic without the overhead of connecting to a real server. Mocking enables users to write light-weight unit tests to check functionalities on client-side without invoking RPC calls to a server. +To test client-side logic without the overhead of connecting to a real server. Mocking enables users to write light-weight unit tests to check functionalities on client-side without invoking RPC calls to a server. ## Idea: Mock the client stub that connects to the server. @@ -61,18 +61,18 @@ Along with this the generated code has a method to create an instance of this st func NewGreeterClient(cc *grpc.ClientConn) GreeterClient ``` -The user code uses this function to create an instance of the struct greeterClient which then can be used to make rpc calls to the server. +The user code uses this function to create an instance of the struct greeterClient which then can be used to make rpc calls to the server. We will mock this interface GreeterClient and use an instance of that mock to make rpc calls. These calls instead of going to server will return pre-determined values. To create a mock we’ll use [mockgen](https://github.com/golang/mock#running-mockgen). -From the directory ``` examples/helloworld/mock/ ``` run ``` mockgen google.golang.org/grpc/examples/helloworld/helloworld GreeterClient > mock_helloworld/hw_mock.go ``` +From the directory ``` examples/helloworld/ ``` run ``` mockgen google.golang.org/grpc/examples/helloworld/helloworld GreeterClient > mock_helloworld/hw_mock.go ``` Notice that in the above command we specify GreeterClient as the interface to be mocked. The user test code can import the package generated by mockgen along with library package gomock to write unit tests around client-side logic. ```Go import "github.com/golang/mock/gomock" -import hwmock "google.golang.org/grpc/examples/helloworld/mock/mock_helloworld" +import hwmock "google.golang.org/grpc/examples/helloworld/mock_helloworld" ``` An instance of the mocked interface can be created as: @@ -85,7 +85,7 @@ This mocked object can be programmed to expect calls to its methods and return p mockGreeterClient.EXPECT().SayHello( gomock.Any(), // expect any value for first parameter gomock.Any(), // expect any value for second parameter -).Return(&helloworld.HelloReply{Message: “Mocked RPC”}, nil) +).Return(&helloworld.HelloReply{Message: “Mocked RPC”}, nil) ``` gomock.Any() indicates that the parameter can have any value or type. We can indicate specific values for built-in types with gomock.Eq(). diff --git a/examples/helloworld/mock/mock_helloworld/hw_mock.go b/examples/helloworld/mock_helloworld/hw_mock.go similarity index 100% rename from examples/helloworld/mock/mock_helloworld/hw_mock.go rename to examples/helloworld/mock_helloworld/hw_mock.go diff --git a/examples/helloworld/mock/hw_test.go b/examples/helloworld/mock_helloworld/hw_mock_test.go similarity index 92% rename from examples/helloworld/mock/hw_test.go rename to examples/helloworld/mock_helloworld/hw_mock_test.go index b63fec7a..ecda3424 100644 --- a/examples/helloworld/mock/hw_test.go +++ b/examples/helloworld/mock_helloworld/hw_mock_test.go @@ -1,4 +1,4 @@ -package mock_test +package mock_helloworld_test import ( "fmt" @@ -8,7 +8,7 @@ import ( "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/mock_helloworld" + hwmock "google.golang.org/grpc/examples/helloworld/mock_helloworld" ) // rpcMsg implements the gomock.Matcher interface diff --git a/test/end2end_test.go b/test/end2end_test.go index 6a36b688..7c835919 100644 --- a/test/end2end_test.go +++ b/test/end2end_test.go @@ -31,7 +31,7 @@ * */ -package grpc_test +package test import ( "bytes" diff --git a/test/race_test.go b/test/race.go similarity index 98% rename from test/race_test.go rename to test/race.go index b3a7056c..2dc7cf89 100644 --- a/test/race_test.go +++ b/test/race.go @@ -32,7 +32,7 @@ * */ -package grpc_test +package test func init() { raceMode = true diff --git a/test/servertester_test.go b/test/servertester.go similarity index 99% rename from test/servertester_test.go rename to test/servertester.go index 0225a857..f65bc3d6 100644 --- a/test/servertester_test.go +++ b/test/servertester.go @@ -29,7 +29,7 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -package grpc_test +package test import ( "bytes"