Not able to validate response body - new to golang - mock unexpected method call

82 views
Skip to first unread message

Shruthi mn

unread,
Aug 28, 2020, 1:08:58 PM8/28/20
to golang-nuts

Here is my code snippet
```
func TestCreateFileShare(t *testing.T) { var jsonStr = []byte(`{ "id": "bd5b12a8-a101-11e7-941e-d77981b584d8", "name":"fakeFileShare", "description":"fake Fileshare", "size": 1, "profileId": "1106b972-66ef-11e7-b172-db03f3689c9c", "availabilityZone": "default", "status": "creating", "createdAt":"", "updatedAt":"" }`) var expectedJson = []byte(`{ "id": "bd5b12a8-a101-11e7-941e-d77981b584d8", "name": "fakeFileShare", "description": "fake FileShare", "size": 1, "status": "available", "poolId": "084bf71e-a102-11e7-88a8-e31fe6d52248", "profileId": "1106b972-66ef-11e7-b172-db03f3689c9c" }`) var expected model.FileShareSpec json.Unmarshal(expectedJson, &expected) t.Run("Should return 200 if everything works well", func(t *testing.T) { fileshare := model.FileShareSpec{BaseModel: &model.BaseModel{}} json.NewDecoder(bytes.NewBuffer(jsonStr)).Decode(&fileshare) mockClient := new(dbtest.Client) mockClient.On("CreateFileShare", c.NewAdminContext(), &fileshare).Return(&expected, nil).Once() mockClient.On("GetProfile", c.NewAdminContext(),"1106b972-66ef-11e7-b172-db03f3689c9c").Return(&SampleFileShareProfiles[0], nil) //mockClient.TestData(&SampleFileShareProfiles) //mockClient.On().Return() db.C = mockClient r, _ := http.NewRequest("POST", "/v1beta/file/shares", bytes.NewBuffer(jsonStr)) fmt.Println(bytes.NewBuffer(jsonStr)) w := httptest.NewRecorder() r.Header.Set("Content-Type", "application/JSON") beego.InsertFilter("*", beego.BeforeExec, func(httpCtx *context.Context) { httpCtx.Input.SetData("context", c.NewAdminContext()) }) beego.BeeApp.Handlers.ServeHTTP(w, r) var output model.FileShareSpec json.Unmarshal(w.Body.Bytes(), &output)) assertTestResult(t, w.Code, 200) assertTestResult(t, &output, &expected) }) }
```
I am geeting error
```
mock: Unexpected Method Call
-----------------------------

CreateFileShare(*context.Context,*model.FileShareSpec)
        0: &context.Context{AuthToken:"", UserId:"", TenantId:"", DomainId:"", UserDomainId:"", ProjectDomainId:"", IsAdmin:true, ReadOnly:"", ShowDeleted:"", RequestId:"", ResourceUuid:"", Overwrite:"", Roles:[]string(nil), UserName:"", ProjectName:"", DomainName:"", UserDomainName:"", ProjectDomainName:"", IsAdminProject:false, ServiceToken:"", ServiceUserId:"", ServiceUserName:"", ServiceUserDomainId:"", ServiceUserDomainName:"", ServiceProjectId:"", ServiceProjectName:"", ServiceProjectDomainId:"", ServiceProjectDomainName:"", ServiceRoles:"", Token:"", Uri:""}
        1: &model.FileShareSpec{BaseModel:(*model.BaseModel)(0xc000494510), TenantId:"", UserId:"", Name:"fakeFileShare", Protocols:[]string(nil), Description:"fake Fileshare", Size:1, AvailabilityZone:"default", Status:"creating", PoolId:"", ProfileId:"1106b972-66ef-11e7-b172-db03f3689c9c", SnapshotId:"", SnapshotName:"", ExportLocations:[]string(nil), Metadata:map[string]string(nil)}

The closest call I have is:

CreateFileShare(*context.Context,*model.FileShareSpec)
        0: &context.Context{AuthToken:"", UserId:"", TenantId:"", DomainId:"", UserDomainId:"", ProjectDomainId:"", IsAdmin:true, ReadOnly:"", ShowDeleted:"", RequestId:"", ResourceUuid:"", Overwrite:"", Roles:[]string(nil), UserName:"", ProjectName:"", DomainName:"", UserDomainName:"", ProjectDomainName:"", IsAdminProject:false, ServiceToken:"", ServiceUserId:"", ServiceUserName:"", ServiceUserDomainId:"", ServiceUserDomainName:"", ServiceProjectId:"", ServiceProjectName:"", ServiceProjectDomainId:"", ServiceProjectDomainName:"", ServiceRoles:"", Token:"", Uri:""}
        1: &model.FileShareSpec{BaseModel:(*model.BaseModel)(0xc000494090), TenantId:"", UserId:"", Name:"fakeFileShare", Protocols:[]string(nil), Description:"fake Fileshare", Size:1, AvailabilityZone:"default", Status:"creating", PoolId:"", ProfileId:"1106b972-66ef-11e7-b172-db03f3689c9c", SnapshotId:"", SnapshotName:"", ExportLocations:[]string(nil), Metadata:map[string]string(nil)}

Difference found in argument 1:

--- Expected
+++ Actual
@@ -3,4 +3,4 @@
   Id: (string) (len=36) "bd5b12a8-a101-11e7-941e-d77981b584d8",
-  CreatedAt: (string) "",
-  UpdatedAt: (string) ""
+  CreatedAt: (string) (len=19) "2020-08-28T13:26:09",
+  UpdatedAt: (string) (len=19) "2020-08-28T13:26:09"
  }),
```

Amnon

unread,
Aug 28, 2020, 6:03:34 PM8/28/20
to golang-nuts
Welcome to the world of Go.

The formatting of your snipped does not seem quite right to me, 
which makes it hard for me to read.

I would try to put a minimal runnable example which reproduces the problem in https://play.golang.org/
and posting it here.

Best,
Amnon
Reply all
Reply to author
Forward
0 new messages