Protocol LYTViewProvider
<pre>
+(NSDictionary) dataSpecForTest;
</pre>
Mock數(shù)據(jù)鸵隧,返回JSON格式的字典缤骨。Example:
+(NSDictionary *)dataSpecForTest {
return @{
@"icon":[[LYTDataValues alloc] initWithValues:@[@"",@"icon_string"]],
@"string":[[LYTDataValues alloc] initWithValues:@[@"預(yù)計(jì)xx送達(dá)",@""]],
};
}
測試次數(shù):2*2 = 4
寫測試的時(shí)候盡量把各種臨界情況覆蓋進(jìn)去,不過在在效果圖的寬高可能固定舍败,或者內(nèi)容一定有的時(shí)候可以酌情不考慮臨界情況。
這里涉及到了這個庫里面自帶的一些數(shù)據(jù)類型比如:
這些類都是用在上面這個字典里面的,我們仔細(xì)看一下LYTStringValues.m
- (NSArray *)values {
return @[
@"Normal length string",
@"",
[NSNull null],
@"Very long string. This string is so long that it's longer than I want it to be. Just a really really long string. In fact, it's just long as long can be. I'm just lengthening the longest string by making it longer with more characters. Do you think this is long enough yet? I think so, so I'm going to stop making this long string longer by adding more characters.",
@"漢語 ? ?? ? ‰ ?? Here are some more special characters ??£??·ú??`?∑?′????√?Ω≥μ??a?"
];
}
這個說明在測試這個屬性對應(yīng)的組件的時(shí)候有5種情況壳快。
所有的這些類都是繼承自LYTDataValues
,重寫其- (NSArray *)values()
即可定義自己項(xiàng)目中所需要的類型。
但是我建議之間用LYTDataValues
類來寫字典中的對象镇草,這樣是比較方便的眶痰。
<pre>
+(UIView *)viewForData:(NSDictionary *)data
reuseView:(UIView *)reuseView
size:(LYTViewSize *)size
context:(id _Nullable __autoreleasing *)context
{}
</pre>
首先要說的是,這個方法會執(zhí)行多次梯啤。如上面所說的4次凛驮,如果你還設(shè)置了屏幕類型,那么還得乘屏幕類型個數(shù):2*2*4(ip4,ip5,ip6,ip6+)条辟。
測試 reuse view,特別是cell黔夭。
data
:dataSpecForTest
這個方法返回的對象
size
:sizesForView
這個方法返回屏幕尺寸信息
<pre>
+ (NSArray<LYTViewSize *> *)sizesForView {
return @[
[[LYTViewSize alloc] initWithWidth:@(LYTiPhone4Width)],
[[LYTViewSize alloc] initWithWidth:@(LYTiPadWidth)]
]
}
</pre>
測試不同大小下的情況,每種尺寸執(zhí)行一次羽嫡”纠眩可以width不變,變height杭棵。也可以height不變邊width婚惫。也可以dynamically set動態(tài)設(shè)置。該方法一定要寫魂爪,因?yàn)橛械娜说牟季质且揽?code>layoutSubView 這個系統(tǒng)方法先舷,如果不設(shè)置,一般是{{0滓侍,0}蒋川,{0,0}}撩笆。
Tips:
1.如果一個大的component有很多小的component組成捺球,建議直接測大的component,因?yàn)檫@樣的測試最好能看到一個完整的效果展示夕冲。
2.有些component屬性需要設(shè)為Public氮兵,我們在.h文件里面把它設(shè)為只讀。
3.測試的時(shí)候歹鱼,比較的兩個視圖必須處于視圖層級的同一級別泣栈。即,相同的父視圖,這一點(diǎn)很重要南片。
4.如果你是在LayoutSubview里面布局篙悯,一定要觸發(fā)該方法,不然是得不到正確的frame的铃绒,一般是{{0鸽照,0},{0颠悬,0}}矮燎。
(本項(xiàng)目內(nèi)部使用)擴(kuò)展:
XCTestCenterYEqual
XCTestCenterXEqual
以上兩個精確到1point
** 測試輔助類**
MSLayoutTestDataHandler
/**
* convert system coordinate
*
* @param superView target view
* @param currentView super view of current view
* @param frame current's frame
*
* @return
*/
- (UIView *)commonSuperView:(UIView \*)superView
currentView:(UIView *)currentView
targetFrame:(CGRect)frame;