activity_test.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. package service
  2. import (
  3. "context"
  4. "fmt"
  5. "reflect"
  6. "testing"
  7. "go-common/app/service/main/point/dao"
  8. "go-common/app/service/main/point/model"
  9. xsql "go-common/library/database/sql"
  10. xtime "go-common/library/time"
  11. "github.com/bouk/monkey"
  12. "github.com/smartystreets/goconvey/convey"
  13. )
  14. //以service层activityGiveTimes方法为例
  15. func TestServiceactivityGiveTimes(t *testing.T) {
  16. convey.Convey("activityGiveTimes", t, func(ctx convey.C) {
  17. //被测方法与桩方法的变量及参数初始化
  18. var (
  19. c = context.Background()
  20. mid = int64(4780461)
  21. changeType = int(3)
  22. point = int64(0)
  23. phs []*model.PointHistory
  24. ph = &model.PointHistory{
  25. ID: 13,
  26. Mid: 4780461,
  27. Point: 60,
  28. ChangeType: 1,
  29. PointBalance: 418,
  30. }
  31. )
  32. phs = append(phs, ph)
  33. //convey包裹调用service测试方法及断言部分
  34. ctx.Convey("When everything goes positive", func(ctx convey.C) {
  35. //使用monkey包构造此service方法下所有依赖的dao层方法
  36. monkey.PatchInstanceMethod(reflect.TypeOf(s.dao), "SelPointHistory", func(_ *dao.Dao, _ context.Context, _ int64, _, _ xtime.Time) ([]*model.PointHistory, error) {
  37. return phs, nil
  38. })
  39. sendTime, err := s.activityGiveTimes(c, mid, changeType, point)
  40. ctx.Convey("Then err should be nil.sendTime should not be nil.", func(ctx convey.C) {
  41. t.Logf("sendTime:%+v", sendTime)
  42. ctx.So(err, convey.ShouldBeNil)
  43. ctx.So(sendTime, convey.ShouldNotBeNil)
  44. })
  45. })
  46. ctx.Convey("When dao return err", func(ctx convey.C) {
  47. //使用monkey包构造此service方法下调dao层失败的情况
  48. monkey.PatchInstanceMethod(reflect.TypeOf(s.dao), "SelPointHistory", func(_ *dao.Dao, _ context.Context, _ int64, _, _ xtime.Time) ([]*model.PointHistory, error) {
  49. return nil, fmt.Errorf("get history err")
  50. })
  51. _, err := s.activityGiveTimes(c, mid, changeType, point)
  52. ctx.Convey("Then err should not be nil", func(ctx convey.C) {
  53. ctx.So(err, convey.ShouldNotBeNil)
  54. })
  55. })
  56. //convey teardown部分(此处UnpatchAll解除所有Patch打桩绑定,确保后续测试流程不被打桩影响)
  57. ctx.Reset(func() {
  58. monkey.UnpatchAll()
  59. })
  60. })
  61. }
  62. func TestServiceactiveSendPoint(t *testing.T) {
  63. convey.Convey("activeSendPoint", t, func(ctx convey.C) {
  64. var (
  65. c = context.Background()
  66. tx *xsql.Tx
  67. phs []*model.PointHistory
  68. ph = &model.PointHistory{
  69. ID: 13,
  70. Mid: 4780461,
  71. Point: 60,
  72. ChangeType: 1,
  73. PointBalance: 418,
  74. }
  75. )
  76. phs = append(phs, ph)
  77. ctx.Convey("When everything goes positive", func(ctx convey.C) {
  78. //activeSendPoint方法中因调用了内部私有activityGiveTimes和updatePoint方法,无法运用monkey反射机制报panic,
  79. // 可以采用给私有方法的下一级打桩或将私有方法转为公有
  80. monkey.PatchInstanceMethod(reflect.TypeOf(s.dao), "SelPointHistory", func(_ *dao.Dao, _ context.Context, _ int64, _, _ xtime.Time) ([]*model.PointHistory, error) {
  81. return phs, nil
  82. })
  83. monkey.PatchInstanceMethod(reflect.TypeOf(s.dao), "InsertPointHistory", func(_ *dao.Dao, _ context.Context, _ *xsql.Tx, ph *model.PointHistory) (int64, error) {
  84. return 0, nil
  85. })
  86. activePoint, err := s.activeSendPoint(c, tx, ph)
  87. ctx.Convey("Then err should be nil.activePoint should not be nil.", func(ctx convey.C) {
  88. t.Logf("activepoint:%+v", activePoint)
  89. ctx.So(err, convey.ShouldBeNil)
  90. ctx.So(activePoint, convey.ShouldNotBeNil)
  91. })
  92. })
  93. ctx.Reset(func() {
  94. monkey.UnpatchAll()
  95. })
  96. })
  97. }