Giter Site home page Giter Site logo

Comments (5)

nickdowell avatar nickdowell commented on June 15, 2024 1

Since we don't provide any APIs to create or notify those kind of events, it's going to be difficult to try and write tests for an onSendError handler.

While it is possible to create a "proper" event by calling Bugsnag.notify and capturing the event, that requires Bugsnag.start to have been called beforehand, which could be awkward for unit testing.

func mockAppHang(unhandled: Bool) -> BugsnagEvent {
    var event: BugsnagEvent!
    // Warning: The block will not be called if Bugsnag has not been started
    Bugsnag.notifyError(NSError(domain: "", code: 0, userInfo: nil)) {
        event = $0
        return false
    }
    event.errors[0].errorClass = "App Hang"
    event.errors[0].errorMessage = """
        The app's main thread failed to respond to an event within 2000 milliseconds
        """
    event.unhandled = unhandled
    return event
}

A possible workaround is to break out some of the onSendError logic into a more testable function, e.g.

func onSendError(event: BugsnagEvent) -> Bool {
    shouldSendError(
        errorClass: event.errors.first?.errorClass,
        unhandled: event.unhandled)
}

// This is more testable since it does not need a BugsnagEvent
func shouldSendError(errorClass: String?, unhandled: Bool) -> Bool {
    if unhandled, errorClass == "App Hang" {
        // TODO: notify without sending to Bugsnag
        return false
    }
    return true
}

from bugsnag-cocoa.

nickdowell avatar nickdowell commented on June 15, 2024

Hi @cwalo this issue is due to BugsnagEvent not implementing a default initializer.

The event created with let event = BugsnagEvent() is missing an internal property that results in the unhandled getter always returning false.

The only supported way to create a BugsnagEvent using our public API is via Bugsnag.notify() or Bugsnag.notifyError():

/// The following test passes
func testUnhandledOverridden() {
    Bugsnag.notifyError(NSError(domain: "", code: 0, userInfo: nil)) { event in
        XCTAssertFalse(event.unhandled)
        event.unhandled = true
        XCTAssertTrue(event.unhandled)
        return false
    }
}

from bugsnag-cocoa.

cwalo avatar cwalo commented on June 15, 2024

Ah, that makes sense. In our case we're interested in being notified on non-fatal (handled) app hang events without reporting them to Bugsnag, so I'm trying to write a unit test that validates the return value of the onSendError block we pass to the BugsnagConfiguration using different BugsnagEvent "types". Even with the public API, I'm not seeing a straightforward way to notify Bugsnag of the same errors that it would report itself (e.g. OOMs, app hangs, thermal kills, etc.), since BugsnagError is not convertible to Error. Ideally, I could do something like this:

func mockAppHang(unhandled: Bool) -> BugsnagEvent {
  let error = BugsnagError()
  error.errorClass = "App Hang"
  let event = BugsnagEvent()
  event.errors = [error]
  // set event.unhandled
       
  return event
}

Do you have any suggestions for writing tests for the onSendError handler utilizing different error events?

from bugsnag-cocoa.

cwalo avatar cwalo commented on June 15, 2024

Nice! That's a decent workaround and I appreciate the suggestion. Obvious in hindsight 😅

from bugsnag-cocoa.

luke-belton avatar luke-belton commented on June 15, 2024

@cwalo I've closed this issue for now, but if you have any other questions just let us know 👍

from bugsnag-cocoa.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.