github.com/tencent/goom@v1.0.1/mocker_test.go (about)

     1  // Package mocker_test 对 mocker 包的测试
     2  // 当前文件实现了对 mocker.go 的单测
     3  package mocker_test
     4  
     5  import (
     6  	"errors"
     7  	"os"
     8  	"testing"
     9  
    10  	"github.com/stretchr/testify/suite"
    11  	mocker "github.com/tencent/goom"
    12  	"github.com/tencent/goom/test"
    13  )
    14  
    15  // TestUnitBuilderTestSuite 测试入口
    16  func TestUnitBuilderTestSuite(t *testing.T) {
    17  	// 开启 debug
    18  	// 1.可以查看 apply 和 reset 的状态日志
    19  	// 2.查看 mock 调用日志
    20  	mocker.OpenDebug()
    21  	suite.Run(t, new(mockerTestSuite))
    22  }
    23  
    24  type mockerTestSuite struct {
    25  	suite.Suite
    26  	fakeErr error
    27  }
    28  
    29  func (s *mockerTestSuite) SetupTest() {
    30  	s.fakeErr = errors.New("fake error")
    31  }
    32  
    33  // TestUnitFuncApply 测试函数 mock apply
    34  func (s *mockerTestSuite) TestUnitFuncApply() {
    35  	s.T().Log("args: ")
    36  	for i := range os.Args {
    37  		s.T().Log(os.Args[i], " ")
    38  	}
    39  	s.Run("success", func() {
    40  		mock := mocker.Create()
    41  		mock.Func(test.Foo).Apply(func(int) int {
    42  			return 3
    43  		})
    44  		s.Equal(3, test.Foo(1), "test.Foo mock check")
    45  
    46  		mock.Reset()
    47  		s.Equal(1, test.Foo(1), "test.Foo mock reset check")
    48  	})
    49  }
    50  
    51  // TestClosureFuncApply 测试闭包函数 mock apply
    52  func (s *mockerTestSuite) TestClosureFuncApply() {
    53  	s.Run("success", func() {
    54  		mock := mocker.Create()
    55  		var r = 1
    56  		mock.Func(test.Foo).Apply(func(int) int {
    57  			return r
    58  		})
    59  		r = 3
    60  		s.Equal(3, test.Foo(1), "test.Foo mock check")
    61  
    62  		mock.Reset()
    63  		s.Equal(1, test.Foo(1), "test.Foo mock reset check")
    64  	})
    65  }
    66  
    67  // TestUnitFuncReturn 测试函数 mock return
    68  func (s *mockerTestSuite) TestUnitFuncReturn() {
    69  	s.Run("success", func() {
    70  		mock := mocker.Create()
    71  		mock.Func(test.Foo).When(1).Return(3)
    72  		s.Equal(3, test.Foo(1), "test.Foo mock check")
    73  
    74  		mock.Reset()
    75  		s.Equal(1, test.Foo(1), "test.Foo mock reset check")
    76  	})
    77  }
    78  
    79  // TestUnitUnexportedFuncApply 测试未导出函数 mock apply
    80  func (s *mockerTestSuite) TestUnitUnexportedFuncApply() {
    81  	s.Run("success", func() {
    82  		mock := mocker.Create()
    83  		mock.Pkg("github.com/tencent/goom/test").ExportFunc("foo").Apply(func(i int) int {
    84  			return i * 3
    85  		})
    86  		s.Equal(3, test.Invokefoo(1), "foo mock check")
    87  
    88  		mock.Reset()
    89  		s.Equal(1, test.Invokefoo(1), "foo mock reset check")
    90  	})
    91  }
    92  
    93  // TestUnitUnexportedFuncReturn 测试未导出函数 mock return
    94  func (s *mockerTestSuite) TestUnitUnexportedFuncReturn() {
    95  	s.Run("success", func() {
    96  		mock := mocker.Create()
    97  		mock.Pkg("github.com/tencent/goom/test").ExportFunc("foo").As(func(i int) int {
    98  			return i * 1
    99  		}).Return(3)
   100  		s.Equal(3, test.Invokefoo(1), "foo mock check")
   101  
   102  		mock.Reset()
   103  		s.Equal(1, test.Invokefoo(1), "foo mock reset check")
   104  	})
   105  }
   106  
   107  // TestUnitMethodApply 测试结构体的方法 mock apply
   108  func (s *mockerTestSuite) TestUnitMethodApply() {
   109  	s.Run("success", func() {
   110  		mock := mocker.Create()
   111  		mock.Struct(&test.Fake{}).Method("Call").Apply(func(*test.Fake, int) int {
   112  			return 5
   113  		})
   114  
   115  		f := &test.Fake{}
   116  		s.Equal(5, f.Call(1), "call mock check")
   117  
   118  		mock.Reset()
   119  		s.Equal(1, f.Call(1), "call mock reset check")
   120  	})
   121  }
   122  
   123  // TestUnitMethodReturn 测试结构体的方法 mock return
   124  func (s *mockerTestSuite) TestUnitMethodReturn() {
   125  	s.Run("success", func() {
   126  		mock := mocker.Create()
   127  		mock.Struct(&test.Fake{}).Method("Call").Return(5).AndReturn(6)
   128  		mock.Struct(&test.Fake{}).Method("Call2").Return(7).AndReturn(8)
   129  
   130  		f := &test.Fake{}
   131  		s.Equal(5, f.Call(1), "call mock check")
   132  		s.Equal(6, f.Call(1), "call mock check")
   133  		s.Equal(7, f.Call2(1), "call mock check")
   134  		s.Equal(8, f.Call2(1), "call mock check")
   135  
   136  		mock.Reset()
   137  		s.Equal(1, f.Call(1), "call mock reset check")
   138  	})
   139  }
   140  
   141  // TestUnitUnExportedMethodApply 测试结构体的未导出方法 mock apply
   142  func (s *mockerTestSuite) TestUnitUnExportedMethodApply() {
   143  	s.Run("success", func() {
   144  		mock := mocker.Create()
   145  		mock.Struct(&test.Fake{}).ExportMethod("call").Apply(func(_ *test.Fake, i int) int {
   146  			return i * 2
   147  		})
   148  
   149  		f := &test.Fake{}
   150  		s.Equal(2, f.Invokecall(1), "call mock check")
   151  
   152  		mock.Reset()
   153  		s.Equal(1, f.Invokecall(1), "call mock reset check")
   154  	})
   155  }
   156  
   157  // TestUnitUnexportedMethodReturn 测试结构体的未导出方法 mock return
   158  func (s *mockerTestSuite) TestUnitUnexportedMethodReturn() {
   159  	s.Run("success", func() {
   160  		mock := mocker.Create()
   161  		mock.Struct(&test.Fake{}).ExportMethod("call").As(func(_ *test.Fake, i int) int {
   162  			return i * 2
   163  		}).Return(6)
   164  
   165  		f := &test.Fake{}
   166  		s.Equal(6, f.Invokecall(1), "call mock check")
   167  
   168  		mock.Reset()
   169  		s.Equal(1, f.Invokecall(1), "call mock reset check")
   170  	})
   171  }
   172  
   173  // TestUnitUnExportStruct 测试未导出结构体的方法 mock apply
   174  func (s *mockerTestSuite) TestUnitUnExportStruct() {
   175  	s.Run("success", func() {
   176  
   177  		// _fake 从 test.fake 中拷贝过来
   178  		type _fake struct {
   179  			_ string // field1
   180  			_ int    // field2
   181  		}
   182  
   183  		mock := mocker.Create()
   184  		// 指定包名
   185  		s.Equal("github.com/tencent/goom_test", mock.PkgName())
   186  
   187  		mock.Pkg("github.com/tencent/goom/test").ExportStruct("*fake").
   188  			Method("call").Apply(func(_ *_fake, i int) int {
   189  			return i * 2
   190  		})
   191  		s.Equal("github.com/tencent/goom_test", mock.PkgName())
   192  
   193  		f := test.NewUnexportedFake()
   194  		s.Equal(2, f.Invokecall(1), "call mock check")
   195  
   196  		mock.Reset()
   197  		s.Equal(1, f.Invokecall(1), "call mock reset check")
   198  	})
   199  }
   200  
   201  // TestMultiReturn 测试调用原函数多返回
   202  func (s *mockerTestSuite) TestMultiReturn() {
   203  	s.Run("success", func() {
   204  		mock := mocker.Create()
   205  		mock.Func(test.Foo).When(1).Return(3).AndReturn(2)
   206  		s.Equal(3, test.Foo(1), "foo mock check")
   207  		s.Equal(2, test.Foo(1), "foo mock check")
   208  
   209  		mock.Reset()
   210  		s.Equal(1, test.Foo(1), "foo mock reset check")
   211  	})
   212  }
   213  
   214  // TestMultiReturns 测试调用原函数多返回
   215  func (s *mockerTestSuite) TestMultiReturns() {
   216  	s.Run("success", func() {
   217  		mock := mocker.Create()
   218  		mock.Func(test.Foo).Returns(2, 3)
   219  		s.Equal(2, test.Foo(1), "foo mock check")
   220  		s.Equal(3, test.Foo(1), "foo mock check")
   221  
   222  		mock.Func(test.Foo).Returns(4, 5)
   223  		s.Equal(4, test.Foo(1), "foo mock check")
   224  		s.Equal(5, test.Foo(1), "foo mock check")
   225  
   226  		mock.Func(test.Foo).When(-1).Returns(6, 7)
   227  		s.Equal(6, test.Foo(-1), "foo mock check")
   228  		s.Equal(7, test.Foo(-1), "foo mock check")
   229  
   230  		mock.Func(test.Foo).When(-2).Returns(8, 9)
   231  		s.Equal(8, test.Foo(-2), "foo mock check")
   232  		s.Equal(9, test.Foo(-2), "foo mock check")
   233  
   234  		mock.Reset()
   235  		s.Equal(1, test.Foo(1), "foo mock reset check")
   236  	})
   237  }
   238  
   239  // TestUnitFuncTwiceApply 测试函数 mock apply 多次
   240  func (s *mockerTestSuite) TestUnitFuncTwiceApply() {
   241  	s.Run("success", func() {
   242  		mock := mocker.Create()
   243  		mock.Func(test.Foo).When(1).Return(3)
   244  		mock.Func(test.Foo).When(2).Return(6)
   245  		s.Equal(3, test.Foo(1), "foo mock check")
   246  		s.Equal(6, test.Foo(2), "foo mock check")
   247  
   248  		mock.Reset()
   249  		mock.Func(test.Foo).When(1).Return(2)
   250  		s.Equal(2, test.Foo(1), "foo mock reset check")
   251  		mock.Reset()
   252  	})
   253  }
   254  
   255  // TestUnitDefaultReturn 测试函数 mock 返回默认值
   256  func (s *mockerTestSuite) TestUnitDefaultReturn() {
   257  	s.Run("success", func() {
   258  		mock := mocker.Create()
   259  		mock.Func(test.Foo).Return(3).AndReturn(4)
   260  		mock.Func(test.Foo).Return(5).AndReturn(6)
   261  		s.Equal(3, test.Foo(1), "foo return check")
   262  		s.Equal(4, test.Foo(2), "foo return check")
   263  		s.Equal(5, test.Foo(1), "foo return check")
   264  		s.Equal(6, test.Foo(2), "foo return check")
   265  		mock.Reset()
   266  	})
   267  }
   268  
   269  // TestFakeReturn 测试返回 fake 值
   270  func (s *mockerTestSuite) TestFakeReturn() {
   271  	s.Run("success", func() {
   272  		mock := mocker.Create()
   273  		defer mock.Reset()
   274  
   275  		mock.Func(test.Foo1).Return(&test.S1{
   276  			Field1: "ok",
   277  			Field2: 2,
   278  		})
   279  		s.Equal(&test.S{
   280  			Field1: "ok",
   281  			Field2: 2,
   282  		}, test.Foo1(), "foo mock check")
   283  	})
   284  }
   285  
   286  func (s *mockerTestSuite) TestUnitNilReturn() {
   287  	s.Run("nil return", func() {
   288  		mocker.Create().Func(test.GetS).Return(nil, s.fakeErr)
   289  		res, err := test.GetS()
   290  		s.Equal([]byte(nil), res)
   291  		s.Equal(s.fakeErr, err)
   292  	})
   293  }
   294  
   295  // TestVarMock 测试简单变量 mock
   296  func (s *mockerTestSuite) TestVarMock() {
   297  	s.Run("simple var mock", func() {
   298  		mock := mocker.Create()
   299  		mock.Var(&test.GlobalVar).Set(2)
   300  		s.Equal(2, test.GlobalVar)
   301  		mock.Reset()
   302  		s.Equal(1, test.GlobalVar)
   303  	})
   304  }
   305  
   306  // TestVarApply 测试变量应用 mock
   307  func (s *mockerTestSuite) TestVarApply() {
   308  	s.Run("var mock apply", func() {
   309  		mock := mocker.Create()
   310  		mock.Var(&test.GlobalVar).Apply(func() int {
   311  			return 2
   312  		})
   313  		s.Equal(2, test.GlobalVar)
   314  		mock.Reset()
   315  		s.Equal(1, test.GlobalVar)
   316  	})
   317  }