You can partially mock the interface. For example I want to simulate AWS S3:

// real S3 client
type realAWSClient struct {
  s3iface.S3API
}

func processMyBuckets(s3 *s3iface.S3API) {
  list := s3.ListBuckets(...)
  // work with list
}

func main() {
  cli := &realAWSClient{S3API: s3.New(...)}
  processMyBuckets(cli)
}

// mock struct in _test.go code
type mockAWSClient struct {
  s3iface.S3API
}

func (mock *mockAWSClient) ListBuckets(*s3.ListBucketsOutput, error) {
   out := createMockOutput()
   return out, nil
}

func TestMyBuckets() {
  cli := &mockAWSClient{}
  processMyBuckets(cli)
}

S3API interface has many methods, but in this case I mock only ListBuckets 
method.
If my code will call other methods then I'll get nil pointer error and I'll 
mock new methods as needed.

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/2a60f3c6-23c3-434c-820d-c23f56347ef2%40googlegroups.com.

Reply via email to