Can someone explain why the two aren't equivalent? The latter does build, but doesn't work as expected. I thought slices would be changed automatically, as contain a pointer to the array.
// Working spec
func TestProcessRecords(t *testing.T) {
var messageSent []*sqs.SendMessageInput
w := &SQSWriter{
queueURL: aws.String("aQueueURL"),
service: &mock.SQS{
SendMessageStub: func(input *sqs.SendMessageInput) (*sqs.SendMessageOutput, error) {
messageSent = append(messageSent, input)
return nil, nil
},
},
}
inputEvent := readFirehoseEventFromFile(t, "../../../../testdata/firehose_event.json")
processRecords(inputEvent.Records, w)
assert.Equal(t, 2, len(inputEvent.Records))
assert.Equal(t, 1, len(messageSent))
}
Attempted refactoring, as the mockedWriter will be used across specs
// Not Working spec
func mockWriter(messageSent []*sqs.SendMessageInput) *SQSWriter{
return &SQSWriter{
queueURL: aws.String("aQueueURL"),
service: &mock.SQS{
SendMessageStub: func(input *sqs.SendMessageInput) (*sqs.SendMessageOutput, error) {
messageSent = append(messageSent, input)
return nil, nil
},
},
}
}
func TestProcessRecords(t *testing.T) {
messageSent := []*sqs.SendMessageInput{}
inputEvent := readFirehoseEventFromFile(t, "../../../../testdata/firehose_event.json")
processRecords(inputEvent.Records, mockWriter(messageSent))
assert.Equal(t, 2, len(inputEvent.Records))
assert.Equal(t, 1, len(messageSent))
}
I should mention that I'm coming from a background in JS/Ruby/Python, and it is taking a bit of time to get a firmer grasp of go fundamentals.
Thanks in advance