本文介绍了如何使用OCMock对象与调用isKindOfClass的代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我想用OCMock测试一些代码。I'd like to test some code with OCMock.代码的内部正在对我提供给代码的mock对象调用 [NSObject isKindOfClass] ,像这样:The innards of the code are calling [NSObject isKindOfClass] on the mock object I'm providing to the code, like so:if ([object isKindOfClass:[FancyClass class]]) { ...}但是,当我提供一个像这样创建的基于OCMockObject的模拟时:However, when I provide an OCMockObject-based mock created like this:mock = [OCMockObject mockForClass:[FancyClass class]];似乎它不通过 isKindOfClass 测试。有任何建议吗?推荐答案如果你调用 isKindOfClass:,并且你没有传递一个plist类(例如NSString或NSNumber),你的做错了。The general rule is that if you're calling isKindOfClass: and you're not passing one of the plist classes (e.g., NSString or NSNumber), You're Doing It Wrong.如果该方法根据它的参数所处的类有两个或多个不同的东西,将其转换为多个方法,并分别测试每个方法。If the method does two or more different things depending on which class its argument is, rend it into multiple methods, and test each method separately.如果方法只有一件事情,但是必须根据不同的类与对象进行不同的交互,那么:If the method does only one thing, but has to interact with the object differently depending on which class it is, then: 制定协议。 (Obj-C中的协议在一些其他OO语言(例如Java)中称为接口。) 使许多类都符合协议。 让测试中的方法检查协议是否符合协议,代替当前的 isKindOfClass:检查。 让测试中的方法使用协议中的方法。Make a protocol. (Protocols in Obj-C are called "interfaces" in some other OO languages, such as Java.)Make the many classes all conform to the protocol. If necessary, use categories to add the necessary methods from outside.Make the method under test check for conformance to the protocol, in place of the current isKindOfClass: check.Make the method under test use the methods in the protocol. 这篇关于如何使用OCMock对象与调用isKindOfClass的代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!