Merge pull request #269 from Ak-Army/master
handle function in mock response
This commit is contained in:
commit
a2eff9918e
@ -80,8 +80,12 @@ func (m *MockClient) Call(ctx context.Context, req client.Request, rsp interface
|
|||||||
if t := reflect.TypeOf(rsp); t.Kind() == reflect.Ptr {
|
if t := reflect.TypeOf(rsp); t.Kind() == reflect.Ptr {
|
||||||
v = reflect.Indirect(v)
|
v = reflect.Indirect(v)
|
||||||
}
|
}
|
||||||
|
response := r.Response
|
||||||
|
if t := reflect.TypeOf(r.Response); t.Kind() == reflect.Func {
|
||||||
|
response = reflect.ValueOf(r.Response).Call([]reflect.Value{})[0].Interface()
|
||||||
|
}
|
||||||
|
|
||||||
v.Set(reflect.ValueOf(r.Response))
|
v.Set(reflect.ValueOf(response))
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -16,6 +16,8 @@ func TestClient(t *testing.T) {
|
|||||||
{Method: "Foo.Bar", Response: map[string]interface{}{"foo": "bar"}},
|
{Method: "Foo.Bar", Response: map[string]interface{}{"foo": "bar"}},
|
||||||
{Method: "Foo.Struct", Response: &TestResponse{Param: "aparam"}},
|
{Method: "Foo.Struct", Response: &TestResponse{Param: "aparam"}},
|
||||||
{Method: "Foo.Fail", Error: errors.InternalServerError("go.mock", "failed")},
|
{Method: "Foo.Fail", Error: errors.InternalServerError("go.mock", "failed")},
|
||||||
|
{Method: "Foo.Func", Response: func() string {return "string"}},
|
||||||
|
{Method: "Foo.FuncStruct", Response: func() *TestResponse {return &TestResponse{Param: "aparam"}}},
|
||||||
}
|
}
|
||||||
|
|
||||||
c := NewClient(Response("go.mock", response))
|
c := NewClient(Response("go.mock", response))
|
||||||
|
Loading…
Reference in New Issue
Block a user