mirror of https://github.com/grpc/grpc-go.git
Fix go buildable source file problem (#1213)
This commit is contained in:
parent
0914b46180
commit
68a5d50f45
|
|
@ -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().
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
@ -31,7 +31,7 @@
|
|||
*
|
||||
*/
|
||||
|
||||
package grpc_test
|
||||
package test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
|
|
|
|||
|
|
@ -32,7 +32,7 @@
|
|||
*
|
||||
*/
|
||||
|
||||
package grpc_test
|
||||
package test
|
||||
|
||||
func init() {
|
||||
raceMode = true
|
||||
|
|
@ -29,7 +29,7 @@
|
|||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
package grpc_test
|
||||
package test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
Loading…
Reference in New Issue