I try to test my CRUD controllers in rest-api. And I have to check the responsed item and expected item in my table-driven test. But I have too much models, and need to compare responded and expected many times with different models, because of it I want implement compare() function. This is my test func, wich check just created items:
func TestViewDevice(t *testing.T) {
var form forms.createDeviceForm//this is my struct to keep responded and expected values
for i, item := range expected { //expected is my table of cases
req, err := http.NewRequest("GET", url, nil)
if err != nil {
t.Fatal(err)
}
resp := httptest.NewRecorder()
routerTest.ServeHTTP(resp, req)
if resp.Code == 200 {
compare(resp.Body.Bytes(), item.reqBody, form)//call to compare
}
But my compare() func doesnt work appropriately:
func compare(resp []byte, item []byte, x interface{}) (bool, error) {
respondedForm:=reflect.ValueOf(x)
expectedForm:=reflect.ValueOf(x)
err := json.Unmarshal(resp, &respondedForm)//does not work
if err != nil {
fmt.Println(err)
}
err = json.Unmarshal(item, &expectedForm)//does not work too
if err != nil {
fmt.Println(err)
}
return reflect.DeepEqual(expectedForm, respondedForm), nil
}
but I can not to unmarshal my response and my test-table values to my forms! json.Unmarshal() return all empty fields of my form... I tried many different ways, but all don't work.
Aucun commentaire:
Enregistrer un commentaire