Skip to content

Commit

Permalink
Added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinrenskers committed Nov 15, 2022
1 parent 0db19e3 commit b124961
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 6 deletions.
10 changes: 6 additions & 4 deletions Tests/SentryTests/SentryClientTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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()
Expand Down
40 changes: 40 additions & 0 deletions Tests/SentryTests/SentryHubTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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, *)
Expand Down
5 changes: 3 additions & 2 deletions Tests/SentryTests/TestClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}

Expand Down

0 comments on commit b124961

Please sign in to comment.