<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                [TOC] ## <span style="font-size:15px">**一、說明**</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;gomonkey 是golang的一款打樁框架,目標是讓用戶在單元測試中低成本的完成打樁,從而將精力聚焦于業務功能的開發。 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;GitHub地址:`https://github.com/agiledragon` ## <span style="font-size:15px">**二、常用法**</span> ### <span style="font-size:15px">**1、為函數打樁**</span> ``` func ApplyFunc(target, double interface{}) *Patches ``` 示例: ``` package util import ( "common/validator" ) type Calculator interface { Add(a, b int) int } type MyCalculator struct{} func (c *MyCalculator) Add(a, b int) int { if err := validator.Validator("verify.json", a); err != nil { return 0 } return a + b } ``` &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;在Add方法中,存在調用外部函數Validator進行參數校驗,當校驗不通過時,返回0。可通過ApplyFunc方法mock掉外部函數的返回值。 ``` func TestAddByGoMonkey(t *testing.T) { m := &MyCalculator{} mockValidator := gomonkey.ApplyFunc(validator.Validator, func(schemaPath string, checkValue interface{}) error { return errors.New("invalid") }) defer func() { mockValidator.Reset() }() if !assert.EqualValues(t, 0, m.Add(1, 2)) { t.Fail() } } ``` ### <span style="font-size:15px">**2、為函數變量打樁**</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;函數變量是一種特殊的變量類型,可以用來存儲函數的引用。它們可以像普通變量一樣被聲明、賦值和調用。 ``` func ApplyFuncVar(target, double interface{}) *Patches ``` ``` package util import ( "common/validator" ) type Calculator interface { Subtract(a, b int) int } type MyCalculator struct{} // 定義一個函數變量subtract,接受兩個int類型的參數,并返回一個int類型的結果 var subtract = func(a, b int) int { return a - b } func (c *MyCalculator) Subtract(a, b int) int { return subtract(a, b) } ``` &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;使用ApplyFuncVar方法mock函數遍歷subtract返回值為2 ``` func TestSubtractByGoMonkey(t *testing.T) { m := &MyCalculator{} mockFuncVar := gomonkey.ApplyFuncVar(&subtract, func(a, b int) int { return 2 }) defer func() { mockFuncVar.Reset() }() if !assert.EqualValues(t, 2, m.Subtract(3, 2)) { t.Fail() } } ``` ### <span style="font-size:15px">**3、為全局變量打樁**</span> ``` func ApplyGlobalVar(target, double interface{}) *Patches ``` 示例: ``` package util import ( "common/validator" ) type Calculator interface { Add(a, b int) int } type MyCalculator struct{} var num = 10 func (c *MyCalculator) Add(a, b int) int { if err := validator.Validator("verify.json", a); err != nil { return num } return a + b } ``` &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;使用ApplyGlobalVar函數mock掉全局變量的值 ``` func TestAddByGoMonkey(t *testing.T) { m := &MyCalculator{} mockGlobalVar := gomonkey.ApplyGlobalVar(&num, 20) mockValidator := gomonkey.ApplyFunc(validator.Validator, func(schemaPath string, checkValue interface{}) error { return errors.New("invalid") }) defer func() { mockValidator.Reset() mockGlobalVar.Reset() }() if !assert.EqualValues(t, 20, m.Add(1, 2)) { t.Fail() } } ``` ### <span style="font-size:15px">**4、為成員方法打樁**</span> ``` func ApplyMethod(target interface{}, methodName string, double interface{}) *Patches ``` 示例: ### <span style="font-size:15px">**4、為成員接口打樁**</span> 示例: ``` // calulator.go package util import ( "github.com/sirupsen/logrus" "common/validator" ) type Calculator interface { Subtract(a, b int) int } type MyCalculator struct{} var subtract = func(a, b int) int { return a - b } func (c *MyCalculator) Subtract(a, b int) int { result := subtract(a, b) rest := validator.NewIRest() return rest.RestOne() } ``` ``` // rest.go package validator type MyRest struct{} type ( IRest interface { RestOne() int } Irest struct{} ) func (i *Irest) RestOne() int { return 7 } func NewIRest() IRest { return &Irest{} } ``` &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;calulator.go的Subtract方法,實例化了rest.go的interface,并調用了RestOne接口。使用ApplyMethod對Irest的成員接口RestOne進行mock,使其返回4, ``` func TestSubtractByGoMonkey(t *testing.T) { m := &MyCalculator{} restMock := gomonkey.ApplyMethod(reflect.TypeOf(validator.NewIRest()), "RestOne", func(_ *validator.Irest) int { return 4 }, ) defer func() { restMock.Reset() }() if !assert.EqualValues(t, 4, m.Subtract(3, 2)) { t.Fail() } } === RUN TestSubtractByGoMonkey INFO[0000] result:1 INFO[0000] RestOne:4 --- PASS: TestSubtractByGoMonkey2 (0.00s) PASS ok sangfor.com/xdr/xlink/common/util 0.045s ```
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看