micro/client/mock/mock_test.go

39 lines
915 B
Go
Raw Normal View History

2016-04-06 20:33:57 +03:00
package mock
import (
2018-03-03 14:53:52 +03:00
"context"
2016-04-06 20:33:57 +03:00
"testing"
"github.com/micro/go-micro/errors"
)
func TestClient(t *testing.T) {
2016-04-06 20:38:33 +03:00
type TestResponse struct {
2016-04-06 20:37:46 +03:00
Param string
}
2016-04-06 20:33:57 +03:00
response := []MockResponse{
{Method: "Foo.Bar", Response: map[string]interface{}{"foo": "bar"}},
2016-04-06 20:38:33 +03:00
{Method: "Foo.Struct", Response: &TestResponse{Param: "aparam"}},
2016-04-06 20:33:57 +03:00
{Method: "Foo.Fail", Error: errors.InternalServerError("go.mock", "failed")},
2018-07-18 02:31:09 +03:00
{Method: "Foo.Func", Response: func() string { return "string" }},
{Method: "Foo.FuncStruct", Response: func() *TestResponse { return &TestResponse{Param: "aparam"} }},
2016-04-06 20:33:57 +03:00
}
c := NewClient(Response("go.mock", response))
for _, r := range response {
2018-04-14 20:06:52 +03:00
req := c.NewRequest("go.mock", r.Method, map[string]interface{}{"foo": "bar"})
2016-04-06 20:37:46 +03:00
var rsp interface{}
2016-04-06 20:33:57 +03:00
err := c.Call(context.TODO(), req, &rsp)
if err != r.Error {
t.Fatalf("Expecter error %v got %v", r.Error, err)
}
t.Log(rsp)
}
}