Skip to content

Commit

Permalink
Merge pull request micro#269 from Ak-Army/master
Browse files Browse the repository at this point in the history
handle function in mock response
  • Loading branch information
Asim Aslam committed Jun 13, 2018
2 parents cd9441f + 52a4705 commit a2eff99
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
6 changes: 5 additions & 1 deletion client/mock/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
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
}
Expand Down
2 changes: 2 additions & 0 deletions client/mock/mock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ func TestClient(t *testing.T) {
{Method: "Foo.Bar", Response: map[string]interface{}{"foo": "bar"}},
{Method: "Foo.Struct", Response: &TestResponse{Param: "aparam"}},
{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))
Expand Down

0 comments on commit a2eff99

Please sign in to comment.