braze-inc / braze-swift-sdk

Braze SDK for the Apple ecosystem, including: iOS, macOS, iPadOS, visionOS, tvOS
https://www.braze.com
Other
52 stars 19 forks source link

[Feature]: Make code using `BrazeDelegate` testable #65

Closed sochalewski closed 1 year ago

sochalewski commented 1 year ago

What problem are you facing?

I successfully migrated from the old Objective-C SDK to the new one, but unfortunately I'm unable to cover with unit tests a class using BrazeDelegate functions.

final class BrazeCampaignCenter: BrazeDelegate {

    private weak var braze: BrazeProtocol?

    func use(braze: BrazeProtocol?) {
        self.braze = braze
        self.braze?.delegate = self
    }

    func braze(_ braze: Braze, shouldOpenURL context: Braze.URLContext) -> Bool {
        context.channel == .inAppMessage
    }
}

This is a very simple example, but it shows exactly what the problem is. In this case I can't write a unit test that executes braze(_:shouldOpenURL:) on the object, because Braze.URLContext cannot be constructed.

The previous SDK used ABKURLDelegate with the following signature:

func handleAppboyURL(
    _ url: URL?,
    from channel: ABKChannel,
    withExtras extras: [AnyHashable: Any]?
) -> Bool

With this all parameters can be set to whatever you want making tests possible.

Workarounds

Not really as both URLContext (Swift) and BRZURLContext (Objective-C) cannot be instantiated by hand.

Ideal Solution

Other Information

SDK: 6.2.0

jerielng commented 1 year ago

Hi @sochalewski, sorry to hear that you are facing this issue. We'll be happy to explore some options in making Braze.URLContext more accessible for you. One alternative we could try is making the initializer for this struct public, so you would be able to initialize it with some mock values. Would this approach be appropriate for your testing?

sochalewski commented 1 year ago

This would help me a lot! 🙌

jerielng commented 1 year ago

@sochalewski Thank you for your input! We'll keep you updated here.

jerielng commented 1 year ago

Hi @sochalewski, just wanted to give an update that we've just released version 6.3.0 of the Braze Swift SDK, which exposes this initializer as public, so you should be able to create a Braze.URLContext object from your end now.

Please let us know if you have any further questions. Thank you!

sochalewski commented 1 year ago

Thanks for solving the problem quickly! I've already restored my tests, everything works as expected. Have a good day!