You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
func TestDownloadFromS3Error1(t *testing.T) {
mockS3Interface := &MockS3Interface{
Error: errors.New("something went wrong on download"),
}
mockS3Interface.On("GetObject", &s3.GetObjectInput{
Bucket: aws.String(testBucket),
Key: aws.String(testKey),
}).Return(nil, "aaaaa").Once()
client := awsclient.AWSClient{
S3Interface: mockS3Interface,
}
downloadFile, _ := os.CreateTemp(os.TempDir(), "testDownloadFromS3-*.txt")
defer downloadFile.Close()
defer os.Remove(downloadFile.Name())
client.DownloadFromS3(testBucket, testKey, downloadFile)
mockS3Interface.AssertExpectations(t)
downloadFileInfo, _ := downloadFile.Stat()
if downloadFileInfo.Size() != 0 {
t.Fatalf("The downloaded file should be empty on S3 failure")
}
expectedLog := fmt.Sprintf("Error downloading from S3:\n%s", mockS3Interface.Error.Error())
assertLogsContain(expectedLog, t)
}
Which is implementing this mocked function:
// GetObject is a mock implementation of the GetObject method
func (m *MockS3Interface) GetObject(input *s3.GetObjectInput) (*s3.GetObjectOutput, error) {
m.Called(input)
if m.Error != nil {
return nil, m.Error
}
return &s3.GetObjectOutput{
Body: io.NopCloser(bytes.NewReader([]byte(m.Body))),
}, nil
}
I notice that the expected return value is not actually being enforced. IE: my MockS3Interface is given the error "something went wrong on download", but the expected error to be returned is "aaaaa" (defined after the .On call). Even though the expected return "aaaa" differs from what is actually returned the test still passes
I know I can do something like args := m.Called(input) to get the values defined in .Return() but shouldn't the expected return values also be enforced similar to how the arguments passed into On()are.
I'm still a noob when it comes to Golang and mocking so this might just be a misunderstanding on my end, but any insight is greatly appreciated. Thanks in advance!
The text was updated successfully, but these errors were encountered:
Hey guys I have this test:
Which is implementing this mocked function:
I notice that the expected return value is not actually being enforced. IE: my MockS3Interface is given the error "something went wrong on download", but the expected error to be returned is "aaaaa" (defined after the .On call). Even though the expected return "aaaa" differs from what is actually returned the test still passes
I know I can do something like
args := m.Called(input)
to get the values defined in.Return()
but shouldn't the expected return values also be enforced similar to how the arguments passed intoOn()
are.I'm still a noob when it comes to Golang and mocking so this might just be a misunderstanding on my end, but any insight is greatly appreciated. Thanks in advance!
The text was updated successfully, but these errors were encountered: