github.com/agiledragon/gomonkey/v2@v2.11.1-0.20240427155748-d56c6823ec17/test/apply_func_var_test.go (about)

     1  package test
     2  
     3  import (
     4  	"testing"
     5  
     6  	. "github.com/agiledragon/gomonkey/v2"
     7  	"github.com/agiledragon/gomonkey/v2/test/fake"
     8  	. "github.com/smartystreets/goconvey/convey"
     9  )
    10  
    11  func TestApplyFuncVar(t *testing.T) {
    12  	Convey("TestApplyFuncVar", t, func() {
    13  
    14  		Convey("for succ", func() {
    15  			str := "hello"
    16  			patches := ApplyFuncVar(&fake.Marshal, func(_ interface{}) ([]byte, error) {
    17  				return []byte(str), nil
    18  			})
    19  			defer patches.Reset()
    20  			bytes, err := fake.Marshal(nil)
    21  			So(err, ShouldEqual, nil)
    22  			So(string(bytes), ShouldEqual, str)
    23  		})
    24  
    25  		Convey("for fail", func() {
    26  			patches := ApplyFuncVar(&fake.Marshal, func(_ interface{}) ([]byte, error) {
    27  				return nil, fake.ErrActual
    28  			})
    29  			defer patches.Reset()
    30  			_, err := fake.Marshal(nil)
    31  			So(err, ShouldEqual, fake.ErrActual)
    32  		})
    33  	})
    34  }