如何对大多数视图使用Firebase身份验证的Swift应用程序进行单元测试?

时间:2018-04-18 16:24:16

标签: swift unit-testing firebase xctest

我使用Firebase在Swift中构建了一个应用程序,其中大多数功能只有在用户登录后才能访问。例如,我有一个预算页面可以从数据库中获取事件预算,但这只能执行一次用户已登录。

是否有人能够建议我如何测试需要调用数据库的功能?我原本以为我需要在我的测试类中签名用户,然后使用这些细节来测试我的控制器,但每次返回nil?我甚至试图在我的测试类中运行我的登录视图控制器来验证用户,但这也不起作用。

谢谢!

1 个答案:

答案 0 :(得分:0)

For Unit tests it is best if you eliminate all outside dependencies. One way that I prefer to do this, especially with any sort of network or database, is to create a Mock object and inject the mock object in instead of using the actual network module or database. This will involve some refactoring for dependency injection and abstracting your manager for your Firebase into a protocol that can be mocked. With a MockFirebase you can have all of the "authentication" without having to use your network or hit the actual database.

If your Firebase manager is set up as a Singleton then a good resource to use for making the Mock of it is:

https://medium.com/@johnsundell/testing-swift-code-that-uses-system-singletons-in-3-easy-steps-89f4884cd325

For information on mocking in general here is another link:

https://www.swiftbysundell.com/posts/mocking-in-swift