From b1249619c46c45f3a97969ea53b23c6994adc739 Mon Sep 17 00:00:00 2001 From: Kevin Renskers Date: Tue, 15 Nov 2022 15:41:10 +0100 Subject: [PATCH] Added tests --- Tests/SentryTests/SentryClientTests.swift | 10 +++--- Tests/SentryTests/SentryHubTests.swift | 40 +++++++++++++++++++++++ Tests/SentryTests/TestClient.swift | 5 +-- 3 files changed, 49 insertions(+), 6 deletions(-) diff --git a/Tests/SentryTests/SentryClientTests.swift b/Tests/SentryTests/SentryClientTests.swift index b8614802a94..3967e5997d3 100644 --- a/Tests/SentryTests/SentryClientTests.swift +++ b/Tests/SentryTests/SentryClientTests.swift @@ -449,8 +449,9 @@ class SentryClientTest: XCTestCase { } func testCaptureErrorWithSession() { - let eventId = fixture.getSut().captureError(error, with: Scope()) { _ in - self.fixture.session + let eventId = fixture.getSut().captureError(error, with: Scope()) { increaseErrorCount in + XCTAssertTrue(increaseErrorCount) + return self.fixture.session } eventId.assertIsNotEmpty() @@ -464,8 +465,9 @@ class SentryClientTest: XCTestCase { func testCaptureErrorWithSession_WithBeforeSendReturnsNil() { let eventId = fixture.getSut(configureOptions: { options in options.beforeSend = { _ in return nil } - }).captureError(error, with: Scope()) { _ in - self.fixture.session + }).captureError(error, with: Scope()) { increaseErrorCount in + XCTAssertFalse(increaseErrorCount) + return self.fixture.session } eventId.assertIsEmpty() diff --git a/Tests/SentryTests/SentryHubTests.swift b/Tests/SentryTests/SentryHubTests.swift index c06bde20805..19f0b14ae74 100644 --- a/Tests/SentryTests/SentryHubTests.swift +++ b/Tests/SentryTests/SentryHubTests.swift @@ -392,6 +392,26 @@ class SentryHubTests: XCTestCase { // only session init is sent XCTAssertEqual(1, fixture.client.captureSessionInvocations.count) } + + func testCaptureWithoutIncreasingErrorCount() { + let sut = fixture.getSut() + sut.startSession() + fixture.client.sessionIncrementsErrorCount = false + sut.capture(error: fixture.error, scope: fixture.scope).assertIsNotEmpty() + + XCTAssertEqual(1, fixture.client.captureErrorWithSessionInvocations.count) + if let errorArguments = fixture.client.captureErrorWithSessionInvocations.first { + XCTAssertEqual(fixture.error, errorArguments.error as NSError) + + XCTAssertEqual(errorArguments.session.errors, 0) + XCTAssertEqual(SentrySessionStatus.ok, errorArguments.session.status) + + XCTAssertEqual(fixture.scope, errorArguments.scope) + } + + // only session init is sent + XCTAssertEqual(1, fixture.client.captureSessionInvocations.count) + } func testCaptureErrorWithoutScope() { fixture.getSut(fixture.options, fixture.scope).capture(error: fixture.error).assertIsNotEmpty() @@ -441,6 +461,26 @@ class SentryHubTests: XCTestCase { // only session init is sent XCTAssertEqual(1, fixture.client.captureSessionInvocations.count) } + + func testCaptureExceptionWithoutIncreasingErrorCount() { + let sut = fixture.getSut() + sut.startSession() + fixture.client.sessionIncrementsErrorCount = false + sut.capture(exception: fixture.exception, scope: fixture.scope).assertIsNotEmpty() + + XCTAssertEqual(1, fixture.client.captureExceptionWithSessionInvocations.count) + if let exceptionArguments = fixture.client.captureExceptionWithSessionInvocations.first { + XCTAssertEqual(fixture.exception, exceptionArguments.exception) + + XCTAssertEqual(exceptionArguments.session.errors, 0) + XCTAssertEqual(SentrySessionStatus.ok, exceptionArguments.session.status) + + XCTAssertEqual(fixture.scope, exceptionArguments.scope) + } + + // only session init is sent + XCTAssertEqual(1, fixture.client.captureSessionInvocations.count) + } @available(tvOS 10.0, *) @available(OSX 10.12, *) diff --git a/Tests/SentryTests/TestClient.swift b/Tests/SentryTests/TestClient.swift index 2836430a90c..c09ab84c0dc 100644 --- a/Tests/SentryTests/TestClient.swift +++ b/Tests/SentryTests/TestClient.swift @@ -84,15 +84,16 @@ class TestClient: Client { return SentryId() } + var sessionIncrementsErrorCount = true var captureErrorWithSessionInvocations = Invocations<(error: Error, session: SentrySession, scope: Scope)>() override func captureError(_ error: Error, with scope: Scope, withSession sessionBlock: @escaping (Bool) -> SentrySession) -> SentryId { - captureErrorWithSessionInvocations.record((error, sessionBlock(true), scope)) + captureErrorWithSessionInvocations.record((error, sessionBlock(sessionIncrementsErrorCount), scope)) return SentryId() } var captureExceptionWithSessionInvocations = Invocations<(exception: NSException, session: SentrySession, scope: Scope)>() override func capture(_ exception: NSException, with scope: Scope, withSession sessionBlock: @escaping (Bool) -> SentrySession) -> SentryId { - captureExceptionWithSessionInvocations.record((exception, sessionBlock(true), scope)) + captureExceptionWithSessionInvocations.record((exception, sessionBlock(sessionIncrementsErrorCount), scope)) return SentryId() }