为了测试 swift对象,从我读过的内容中,我们对它们进行子类化并模拟我们想要的方法返回我们的测试值.然后,我观看了一个关于快速性能的WWDC视频,并且演示者建议将类标记为最终,以帮助
我遇到的问题如何将类标记为final,但仍然允许子类模拟?有没有人真正遇到这个问题或者我应该从我的声明中删除最后一个关键字?
任何建议都很棒,或者没有任何建议可以告诉我,我做得不对.
谢谢,
麦克风.
internal protocol MockableProtocol {
func deleteItem(_ itemId: String) -> Bool
func fetchAllItems() -> [CustomObject]
func fetchItem(for id: String) -> CustomObject?
}
internal final class MyFinalClass: MockableProtocol {
func deleteItem(_ itemId: String) -> Bool {
// Your code here
}
func fetchAllItems() -> [CustomObject] {
// Your code here
}
func fetchItem(for id: String) -> CustomObject? {
// Your code here
}
}
然后在测试中:
class TestMockClass: MockableProtocol {
func deleteItem(_ itemId: String) -> Bool {
// Your code here
}
func fetchAllItems() -> [CustomObject] {
// Your code here
}
func fetchItem(for id: String) -> CustomObject? {
// Your code here
}
}
