Golang中如何为AddVector创建Mock函数

Golang中如何为AddVector创建Mock函数

type VectorService interface {
    AddVector() [2]int
}

type InitService struct{
         x    int
	     y    int
}

type MyService struct {
    VService VectorService
}

func (sms InitService) AddVector() [2]int {
    var b [2]int
	b[0] = sms.x + 100
	b[1] = sms.y + 200
    return b
}

func (a MyService) ChargeVector(x int, y int) [2]int {
    r := a.VService.AddVector()
	var b [2]int
	b[0] = r[0] + 10 
	b[1] = r[1] + 22
    return b
}

func main() {
    myService := MyService{InitService{1,2}}
    a := myService.ChargeVector(100, 200)
	fmt.Printf("Charging Customer For the value of %d  %d\n", a[0], a[1])
}

更多关于Golang中如何为AddVector创建Mock函数的实战教程也可以访问 https://www.itying.com/category-94-b0.html

3 回复

这个问题我已经解决了。非常感谢!

更多关于Golang中如何为AddVector创建Mock函数的实战系列教程也可以访问 https://www.itying.com/category-94-b0.html


测试通过,但现在我想在 InitServiceMock 中使用 X 和 Y 字段,该如何实现?

   type InitServiceMock struct {
          mock.Mock
   }

   func (sms *InitServiceMock) AddToVector() [2]int {
    fmt.Println("Mocked charge notification function")
	fmt.Printf("Charging Customer For the value of -- %d --  %d -- \n", 100, 200)
	

	var b [2]int
 	b[0] = 100
	b[1] = 200

	return  b
   }

    func TestChargeCustomer(t *testing.T) {

   	var b [2]int
   	b[0] = 101
  	b[1] = 202
	
	smsService := new(InitServiceMock)
   	smsService.On("AddToVector").Return(b)
	
 	myService := test.MyService{test.InitService{1,2}}
    a := myService.ChargeVector(100,200)

    assert.Equal(t, a[0], 111, "One")
	assert.Equal(t, a[1], 224, "Two")
	
	fmt.Printf("Charging Customer For the value of %d  %d\n", a[0], a[1])
   }

在Go语言中为AddVector创建Mock函数,可以使用接口和依赖注入的方式。以下是使用gomock框架的示例:

// 定义接口
type VectorService interface {
    AddVector() [2]int
}

// 生产代码中的具体实现
type InitService struct {
    x int
    y int
}

func (sms InitService) AddVector() [2]int {
    var b [2]int
    b[0] = sms.x + 100
    b[1] = sms.y + 200
    return b
}

// 业务逻辑
type MyService struct {
    VService VectorService
}

func (a MyService) ChargeVector(x int, y int) [2]int {
    r := a.VService.AddVector()
    var b [2]int
    b[0] = r[0] + 10 
    b[1] = r[1] + 22
    return b
}

// 使用gomock生成mock
// 首先安装gomock: go install go.uber.org/mock/mockgen@latest
// 生成mock代码: mockgen -source=your_file.go -destination=mock_vector_service.go -package=yourpackage

// 测试代码示例
import (
    "testing"
    "github.com/golang/mock/gomock"
)

func TestMyService_ChargeVector(t *testing.T) {
    ctrl := gomock.NewController(t)
    defer ctrl.Finish()
    
    // 创建mock对象
    mockVectorService := NewMockVectorService(ctrl)
    
    // 设置期望行为
    mockVectorService.EXPECT().
        AddVector().
        Return([2]int{150, 250}).
        Times(1)
    
    // 注入mock依赖
    myService := MyService{VService: mockVectorService}
    
    // 执行测试
    result := myService.ChargeVector(100, 200)
    
    // 验证结果
    expected := [2]int{160, 272}
    if result != expected {
        t.Errorf("Expected %v, got %v", expected, result)
    }
}

如果不想使用gomock,也可以手动创建mock:

// 手动mock实现
type MockVectorService struct {
    AddVectorFunc func() [2]int
}

func (m *MockVectorService) AddVector() [2]int {
    if m.AddVectorFunc != nil {
        return m.AddVectorFunc()
    }
    return [2]int{0, 0}
}

// 测试用例
func TestMyService_ChargeVector_ManualMock(t *testing.T) {
    mockService := &MockVectorService{
        AddVectorFunc: func() [2]int {
            return [2]int{150, 250}
        },
    }
    
    myService := MyService{VService: mockService}
    result := myService.ChargeVector(100, 200)
    
    expected := [2]int{160, 272}
    if result != expected {
        t.Errorf("Expected %v, got %v", expected, result)
    }
}

使用stub的另一种方式:

type StubVectorService struct{}

func (s StubVectorService) AddVector() [2]int {
    // 返回固定的测试值
    return [2]int{150, 250}
}

func TestMyService_ChargeVector_Stub(t *testing.T) {
    myService := MyService{VService: StubVectorService{}}
    result := myService.ChargeVector(100, 200)
    
    expected := [2]int{160, 272}
    if result != expected {
        t.Errorf("Expected %v, got %v", expected, result)
    }
}

这些示例展示了如何使用不同的mock策略来测试MyService.ChargeVector方法,而不依赖真实的InitService实现。

回到顶部