我试图了解什么是可模拟的,什么不是。
在使用 NSMutableAttributedString 的实验中,我似乎无法模拟initWithAttributedString
.
- (void)test_mutableString_shouldWorkAsAMutableString {
NSMutableAttributedString *_mutable = [OCMockObject mockForClass:NSMutableAttributedString.class];
NSAttributedString *_string = [OCMockObject mockForClass:NSAttributedString.class];
[[[(id)_mutable expect] andReturnValue:nil] initWithAttributedString:_string];
[_mutable initWithAttributedString:_string];
}
此代码不会运行;由于某种原因,可变屏幕的代理无法识别initWithAttributedString
选择器:
2013-03-12 11:25:30.725 UnitTests[11316:c07] TestItClass/test_4_mutableString_shouldWorkAsAMutableString ✘ 0.00s
Name: NSInvalidArgumentException
File: Unknown
Line: Unknown
Reason: *** -[NSProxy doesNotRecognizeSelector:initWithAttributedString:] called!
0 CoreFoundation 0x01c0602e __exceptionPreprocess + 206
1 libobjc.A.dylib 0x01948e7e objc_exception_throw + 44
2 CoreFoundation 0x01c05deb +[NSException raise:format:] + 139
3 Foundation 0x00862bcd -[NSProxy doesNotRecognizeSelector:] + 75
4 CoreFoundation 0x01bf5bbc ___forwarding___ + 588
5 CoreFoundation 0x01bf594e _CF_forwarding_prep_0 + 14
6 UnitTests 0x00349e0b -[TestItClass test_4_mutableString_shouldWorkAsAMutableString] + 283
我试图了解如何可靠地使用 OCMock,但这让我感到困惑,我不确定哪些 OCMock 调用可以工作,哪些不应该工作。
我非常感谢对此进行澄清,并提示为什么上述方法不起作用。
谢谢,乔