Please help my make Mock Function to AddVector

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])
}

Test PASS, but now I want use InitServiceMock with X and Y filed How I can do it?

   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])
   }

I solved this problem. Thank you a lot!

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.