github.com/agiledragon/gomonkey/v2@v2.11.1-0.20240427155748-d56c6823ec17/test/apply_interface_reused_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 TestApplyInterfaceReused(t *testing.T) { 12 e := &fake.Etcd{} 13 14 Convey("TestApplyInterfaceReused", t, func() { 15 patches := ApplyFunc(fake.NewDb, func(_ string) fake.Db { 16 return e 17 }) 18 defer patches.Reset() 19 db := fake.NewDb("mysql") 20 21 Convey("TestApplyInterface", func() { 22 info := "hello interface" 23 patches.ApplyMethod(e, "Retrieve", 24 func(_ *fake.Etcd, _ string) (string, error) { 25 return info, nil 26 }) 27 output, err := db.Retrieve("") 28 So(err, ShouldEqual, nil) 29 So(output, ShouldEqual, info) 30 }) 31 32 Convey("TestApplyInterfaceSeq", func() { 33 info1 := "hello cpp" 34 info2 := "hello golang" 35 info3 := "hello gomonkey" 36 outputs := []OutputCell{ 37 {Values: Params{info1, nil}}, 38 {Values: Params{info2, nil}}, 39 {Values: Params{info3, nil}}, 40 } 41 patches.ApplyMethodSeq(e, "Retrieve", outputs) 42 output, err := db.Retrieve("") 43 So(err, ShouldEqual, nil) 44 So(output, ShouldEqual, info1) 45 output, err = db.Retrieve("") 46 So(err, ShouldEqual, nil) 47 So(output, ShouldEqual, info2) 48 output, err = db.Retrieve("") 49 So(err, ShouldEqual, nil) 50 So(output, ShouldEqual, info3) 51 }) 52 53 Convey("the arg type can be interface", func() { 54 info := "hello interface" 55 patches.ApplyMethod(e, "Retrieve", 56 func(_ fake.Db, _ string) (string, error) { 57 return info, nil 58 }) 59 output, err := db.Retrieve("") 60 So(err, ShouldEqual, nil) 61 So(output, ShouldEqual, info) 62 }) 63 }) 64 }