Skip to content

Commit ed9f97b

Browse files
committed
Add test case for Fatal method in defaultLogger
1 parent be5273c commit ed9f97b

File tree

1 file changed

+15
-12
lines changed

1 file changed

+15
-12
lines changed

logger/zaplogger_logger_test.go

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ func TestDefaultLogger_Panic(t *testing.T) {
146146
mockLogger.AssertExpectations(t)
147147
}
148148
*/
149+
149150
// TestDefaultLogger_Fatal verifies the Fatal method of the defaultLogger struct.
150151
// It ensures that Fatal logs messages at the Fatal level and exits the application with a non-zero status code.
151152
// The test utilizes a mockLogger to capture and assert the call to the zap.Logger's Fatal method.
@@ -154,23 +155,25 @@ func TestDefaultLogger_Fatal(t *testing.T) {
154155
dLogger := &defaultLogger{logger: mockLogger.Logger, logLevel: LogLevelFatal}
155156

156157
expectedFatalMsg := "fatal message"
158+
// Use mock.Anything for fields argument to avoid strict matching issues
157159
mockLogger.On("Fatal", expectedFatalMsg, mock.Anything).Once()
158160

159-
// Since Fatal exits the application, we use a sub-test to capture the exit status
160-
t.Run("TestFatal", func(t *testing.T) {
161-
// Replace os.Exit temporarily to capture exit status
162-
oldExit := osExit
163-
defer func() { osExit = oldExit }()
164-
var exitStatus int
165-
osExit = func(code int) {
166-
exitStatus = code
167-
}
161+
// Replace os.Exit temporarily to capture exit status
162+
oldExit := osExit
163+
var exitStatus int
164+
osExit = func(code int) {
165+
exitStatus = code
166+
}
167+
// Ensure the original os.Exit is restored after this test
168+
defer func() { osExit = oldExit }()
168169

169-
dLogger.Fatal(expectedFatalMsg)
170+
// Execute the Fatal method, which is expected to call os.Exit
171+
dLogger.Fatal(expectedFatalMsg)
170172

171-
assert.Equal(t, 1, exitStatus, "Expected non-zero exit status")
172-
})
173+
// Verify the exit status is set as expected
174+
assert.Equal(t, 1, exitStatus, "Expected non-zero exit status")
173175

176+
// Confirm that the mock logger's expectations are met
174177
mockLogger.AssertExpectations(t)
175178
}
176179

0 commit comments

Comments
 (0)