how to test functions that return a channel in golang code example

Example: how to test functions that return a channel in golang

package main

import (
	"reflect"
	"testing"
)

func Test_generateInts(t *testing.T) {
	tests := []struct {
		name string
		want []int
	}{
		{
			name: "default testing case",
			want: []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9},
		},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			cgot := generateInts()
			var got []int

			for i := range cgot {
				got = append(got, i)
			}

			if !reflect.DeepEqual(got, tt.want) {
				t.Errorf("generateInts() = %v, want %v", got, tt.want)
			}
		})
	}
}

Tags:

Misc Example