ovechkin-dm / mockio

Mockito for golang
MIT License
73 stars 2 forks source link

Matcher for variadic function support? Is it supported for variadic params for now? #41

Closed gimon1156 closed 8 months ago

gimon1156 commented 8 months ago

Hi I have following code but don't know how to match the method called to mock the result.

 type Greet interface {
      sayHello(welcomeMsg string, names ...string) string
  }

  type GreetService struct {
  }

  func (h *GreetService) sayHello(welcomeMsg string, names ...string) string {
      result := welcomeMsg
      for _, name := range names {
          result += fmt.Sprintf(" %s", name)
      }
      return result
  }

  func TestSayHello(t *testing.T) {
      SetUp(t)

      greet := Mock[Greet]()
      WhenSingle(greet.sayHello(Any[string]())).ThenReturn("hello James")

      result := greet.sayHello("hello")
      assert.Equal(t, result, "hello James")

      WhenSingle(greet.sayHello(Any[string](), Any[WHAT_TO_PUT_HERE])).ThenReturn("hello James Jack")

      result = greet.sayHello("hello", "James", "Jack")
      assert.Equal(t, result, "hello James Jack")
}

The names params inside the sayHello is optional and can take in multiple string value. Try with Any[string]() but not working.

Both above mocking is not working

ovechkin-dm commented 8 months ago

Hi! I think I completely forgot about variadic functions support. I will check it and apply fix if needed.

ovechkin-dm commented 8 months ago

Varargs are working now. So in your example it should be smth like WhenSingle(greet.sayHello(Any[string](), Any[string](), Any[string]())).ThenReturn("hello James Jack")