0

Assume that I have a function called GetQueue with GoLang and depends on the configuration, it would return different types of queue implementations.

func GetQueue(config string) *service.Queue {
switch(config):
case "A":
  return &QueueA.NewQueue{...}
case "B":
  return &QueueB.NewQueue{...}
...
}

The service.Queue is an interface. And both QueueA and QueueB are implementations for this interface. The QueueA.go looks like:

type service struct {
...
}

function NewService() *service.Queue {
  return &service{}
}

I'd like to write a unit test for the GetQueue function (which is defined outside of the service package) and I would like to verify the returned types are as expected. However, both QueueA.sevice and QueueB.service are private structs. So how can I verify the returned types are the expected ones?

injoy
  • 3,993
  • 10
  • 40
  • 69
  • White box testing, test inside the package! – ifnotak Apr 15 '19 at 22:59
  • 1
    This code makes no sense. NewService isn't called anywhere and its return type doesn't match what it actually returns. Also, `function` isn't a keyword in Go. Try to make an example that accurately represents your real code, otherwise it's hard to see what your actual problem is. – Peter Apr 16 '19 at 05:08
  • 1
    I'm a little bit confused.Is `service.Queue`'s type interface{}? Are `return &QueueA.NewQueue{...}` inside `GetQueue` and `NewService()` inside QueueA.go the same function? – SpiderShrimp Apr 16 '19 at 02:59
  • @SpiderShrimp Sorry, I did't make it clear enough. I've revised my questions a bit. – injoy Apr 16 '19 at 07:05
  • @Peter Sorry, I did't make it clear enough. I've revised my questions a bit. – injoy Apr 16 '19 at 07:06

0 Answers0