问题描述
已经编写了中对它的纸张选择功能进行单元测试。 p>
其声明为:
可选功能func printInteractionController(_ printInteractionController(_ printInteractionController:UIPrintInteractionController,选择纸张文件列表:[UIPrintPaper])-> UIPrintPaper
用预定义的UIPrintPaper值调用它并检查输出是一个简单的问题。但是,我无法创建UIPrintPaper实例。 UIPrintPaper的声明方式如下:
NS_CLASS_AVAILABLE_IOS(4_2)__ TVOS_PROHIBITED @interface UIPrintPaper:NSObject
+ (UIPrintPaper *)bestPaperForPageSize:(CGSize)contentSize withPapersFromArray:(NSArray< UIPrintPaper *> *)paperList; //供委托使用。传入列表
@property(只读)CGSize paperSize;
@property(readonly)CGRect printableRect;
@end
paperSize和printableRect属性为只读,没有初始化程序来定义它们。如何创建UIPrintPaper来代表测试的不同纸张尺寸? (A4,美国字母,4x6 ...)
无法控制UIPrintPaper,但是将其子类化以覆盖其只读属性是直接的:
class FakePrintPaper:UIPrintPaper {
private let size:CGSize
得以覆盖var paperSize:CGSize {返回尺寸}
覆盖var printableRect:CGRect {返回CGRect(来源:CGPointZero,大小:size)}
init(size:CGSize){
self .size =大小
}
}
Having written a UIPrintInteractionControllerDelegate, I wish to unit test its paper selection functionality in printInteractionController:choosePaper:
Its declaration is:
optional func printInteractionController(_ printInteractionController: UIPrintInteractionController, choosePaper paperList: [UIPrintPaper]) -> UIPrintPaper
It is a simple matter of calling it with predefined UIPrintPaper values and checking the output. However I am unable to create UIPrintPaper instances. Here is how UIPrintPaper is declared:
NS_CLASS_AVAILABLE_IOS(4_2)__TVOS_PROHIBITED @interface UIPrintPaper : NSObject + (UIPrintPaper *)bestPaperForPageSize:(CGSize)contentSize withPapersFromArray:(NSArray<UIPrintPaper *> *)paperList; // for use by delegate. pass in list @property(readonly) CGSize paperSize; @property(readonly) CGRect printableRect; @end
The paperSize and printableRect properties are readonly and there is no initializer to define them. How can I create UIPrintPaper to represent different paper sizes for my tests? (A4, US Letter, 4x6...)
Can't control UIPrintPaper, but subclassing it to override its readonly properties is straighforward:
class FakePrintPaper: UIPrintPaper { private let size: CGSize override var paperSize: CGSize { return size } override var printableRect: CGRect { return CGRect(origin: CGPointZero, size: size) } init(size: CGSize) { self.size = size } }
这篇关于如何创建UIPrintPaper来测试UIPrintInteractionControllerDelegate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!