【问题标题】:How can I mock Firebase RemoteConfig for Test in iOS Swift?如何在 iOS Swift 中模拟 Firebase RemoteConfig 以进行测试?
【发布时间】:2021-03-15 09:41:27
【问题描述】:

我有一个接受 Firebase RemoteConfig 的函数

func myfunction(remoteConfig: RemoteConfig) -> String {
    if (remoteConfig["my_value"].boolValue) {
        return "ABC"
    } else {
        return "DEF"
    }
}

我尝试模拟RemoteConfig,但不知道该怎么做。 我尝试如下,显然,它不起作用,并投诉

Cannot override 'init' which has been marked unavailable: Use +remoteConfig instead.

class MockRemoteConfig: RemoteConfig {
    public init() {}
}

class FirebaseServicesSpec : QuickSpec {
    override func spec() {
        describe("myfunction") {
            context("when my_value is not set") {
                it("is should return DEF") {
                    expect(FirebaseServices.myfunction(
                            remoteConfig: MockRemoteConfig())).to(equal("DEF"))
                }
            }
        }
    }
}

如何在 Swift 中模拟 RemoteConfig? (在 Android 中,我可以使用 Mock())。

【问题讨论】:

    标签: ios swift firebase-remote-config


    【解决方案1】:

    我遇到了同样的问题,我发现这样做的唯一方法是生成一个协议,克隆我计划从 RemoteConfig 使用的方法的签名,在你的情况下,你所需要的只是一个定义的协议下标函数

    protocol RemoteConfigProtocol {
        func configValue(forKey key: String?) -> RemoteConfigValue // Used internally to fetch values
        subscript(_ key: String?) -> RemoteConfigValue { get } // Used to index by key like remoteConfig[key]
    }
    
    extension RemoteConfigProtocol {
        subscript(key: String?) -> RemoteConfigValue {
            return self.configValue(forKey: key)
        }
    }
    

    然后用它扩展RemoteConfig

    extension RemoteConfig: RemoteConfigProtocol {
        // Already complies since we are cloning RemoteConfig signatures
    }
    

    之后你应该直接使用协议而不是RemoteConfig

    func myfunction(remoteConfig: RemoteConfigProtocol) -> String {
        if (remoteConfig["my_value"].boolValue) {
            return "ABC"
        } else {
            return "DEF"
        }
    }
    

    用于测试

    class MockRemoteConfig: RemoteConfigProtocol {
        // We have init available!
    
        // Protocol stubs go here
    }
    

    希望你觉得这很有用。

    【讨论】:

    • 如何强制 MockRemoteConfig 中的键值?
    【解决方案2】:

    关注Jose's answer。以RemoteConfigProtocol开头

    protocol RemoteConfigProtocol {
        func configValue(forKey key: String?) -> RemoteConfigValue // Used internally to fetch values
        subscript(_ key: String?) -> RemoteConfigValue { get } // Used to index by key like remoteConfig[key]
    }
    
    extension RemoteConfigProtocol {
        subscript(key: String?) -> RemoteConfigValue {
            return self.configValue(forKey: key)
        }
    }
    

    我为RemoteConfigValue添加了模拟

    import Firebase
    
    class RemoteConfigValueMock: RemoteConfigValue {
    
        var stringMock: String?
        var boolMock: Bool = false
    
        override var stringValue: String? { stringMock }
        override var boolValue: Bool { boolMock }
    }
    
    

    把它当作

    class MockRemoteConfig: RemoteConfigProtocol {
        func configValue(forKey key: String?) -> RemoteConfigValue {
          let value = RemoteConfigValueMock()
          value.stringMock = "value"
        return value
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-04-25
      • 2015-07-27
      • 1970-01-01
      • 2023-02-12
      • 2011-08-03
      • 2022-01-22
      • 2017-03-22
      • 1970-01-01
      相关资源
      最近更新 更多