github.com/go-board/x-go@v0.1.2-0.20220610024734-db1323f6cb15/xctx/typed_context_test.go (about)

     1  package xctx
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/require"
     8  )
     9  
    10  type User struct {
    11  	Name string
    12  }
    13  
    14  func TestRead(t *testing.T) {
    15  	ctx := NewTyped(context.Background())
    16  	ctx.With(User{Name: "lixiaohui"})
    17  	user, ok := ctx.ReadTyped(User{})
    18  	require.True(t, ok)
    19  	realUser, ok := user.(User)
    20  	require.True(t, ok)
    21  	require.Equal(t, "lixiaohui", realUser.Name)
    22  }