-1

I'm trying to execute unit test for below function but it not completely executing all line.

func (a API) FetchingTextFile() gin.HandlerFunc {
    return func(c *gin.Context) {
        f := c.Param("fileType")

        files, _, err := a.grid.ListObjects(a.gridBucket, f+"/")
        if err != nil {
            log.Error().Caller().Err(err).Msgf("failed to retrieve files )
            return
        }

        c.JSON(http.StatusOK, files)
    }
}

its not executing after line *return func(c gin.Context) { why is that so any thoughts on same.

Test cases added below,

func Test_FetchingTextFile(t *testing.T) {
    type fields struct {
        grid:             tt.fields.grid,
    }
    tests := []struct {
        name   string
        fields fields
        want   gin.HandlerFunc
    }{
        {
            name: "Get file test",
                grid: &AgentMocking{
                    err: nil,
                },
            },
        },
    }
    for _, tt := range tests {
        t.Run(tt.name, func(t *testing.T) {
            a := API{
                grid:             tt.fields.grid,
            }
            
            // just for checking context
            ctx := r.Context()
            ctx = context.WithValue(ctx, "fileType", "text")
            r = r.WithContext(ctx)

            if got := a.FetchingTextFile(); !reflect.DeepEqual(got, tt.want) {
                t.Errorf("FetchingTextFile() = %v, want %v", got, tt.want)
            }
        })
    }
}
ROHITH P
  • 17
  • 3
  • The `FetchingTextFile` function returns another function. You are comparing a function with something else using `DeepEqual` – Burak Serdar Aug 07 '23 at 19:50
  • If you call `FetchingTextFile` you are executing the return statement. You cannot compare functions (they can only be compared to `nil`), and you definitely cannot compare function implementations. – JimB Aug 07 '23 at 19:53
  • My concern is about as I'm debugging line by line for called FetchingTextFile() function but its not entering after line return func(c *gin.Context) { – ROHITH P Aug 07 '23 at 19:55
  • @ROHITHP: it's not entering the line after the function declaration, because you are not calling the function. – JimB Aug 07 '23 at 19:55
  • Yes I' know functions cannot compare . I just wanted check ,whether can we see execution of each line for FetchingTextFile return func. – ROHITH P Aug 07 '23 at 19:58
  • 1
    You have to call that function for it to execute, i,e,: `FetchingTextFile()(ctx)` – Burak Serdar Aug 07 '23 at 20:07
  • A common definition of equality of functions is via extensionality: Compare selected values. – Volker Aug 07 '23 at 20:59

0 Answers0