I'm attempting to test a method on my class that calls a class method on another class. The second class I'm calling is contained inside of a framework if that matters.
我正在尝试在我的类上测试一个方法,该方法在另一个类上调用类方法。如果重要的话,我正在调用的第二个类包含在框架内。
Simplified version of the method I'm trying to test. I just want to verify that the switch statement does what I expect by catching and verifying the call to [DataCapture trackEvent:].
我正在尝试测试的方法的简化版本。我只是想通过捕获和验证对[DataCapture trackEvent:]的调用来验证switch语句是否符合我的预期。
- (void)beaconValue:(NSInteger)value
{
NSString* elementIdValue;
switch(value)
{
case 1:
elementIdValue = @"One";
break;
case 2:
elementIdValue = @"Two";
break;
case 3:
elementIdValue = @"Three";
break;
}
[DataCapture trackEvent:elementValueId];
}
Here is the test I wrote that I expect to work:
这是我写的测试,我希望工作:
- (void)testCaptureData_1
{
id mockDataCapture = OCMClassMock([DataCapture class]);
OCMExpect([mockDataCapture trackEvent:@"One"]);
[[BeaconingService sharedBeaconingService] beaconValue:1];
OCMVerifyAll(mockDataCapture);
[mockDataCapture stopMocking];
}
My verify always tells me that the expected trackEvent method was not invoked, even if I change my expect to [OCMArg any]. Am I doing something obviously wrong or is the problem elsewhere (i.e. bad project setup?)
我的验证总是告诉我,即使我将期望改为[OCMArg any],也没有调用预期的trackEvent方法。我做的事情显然是错误的还是其他地方的问题(即糟糕的项目设置?)
2 个解决方案
#1
0
This looks good and should work. I can only think of two reasons for the behaviour you observe.
这看起来不错,应该有效。我只能想到你观察到的行为有两个原因。
If the DataCapture
class has an instance method that is also called trackEvent:
then the mock will mock the instance method and not the class method. In such cases it is necessary to wrap the method invocation in a ClassMethod()
macro, i.e. OCMExpect(ClassMethod(...)))
.
如果DataCapture类有一个也称为trackEvent的实例方法:那么mock将模拟实例方法而不是类方法。在这种情况下,有必要将方法调用包装在ClassMethod()宏中,即OCMExpect(ClassMethod(...)))。
If that's not the case my next guess would be an issue with CocoaPods. I've seen several similar reports and in the end it turned out that CocoaPods would somehow add the same binary twice. In such cases it's possible that OCMock adds its magic to one version of the class but the test uses another. I don't have a reference handy but this shouldn't be too hard to find.
如果情况并非如此,我的下一个猜测将是CocoaPods的问题。我已经看过几个类似的报告,最后发现CocoaPods会以某种方式两次添加相同的二进制文件。在这种情况下,OCMock可能会将其魔力添加到类的一个版本中,但测试使用另一个版本。我没有参考方便,但这不应该太难找到。
#2
0
Just incase anyone runs across a similar issue I was able to solve my problem by adding the -bind_at_load -ObjC
other linker flags to my test target.
只是因为任何人遇到类似问题我都能通过将-bind_at_load -ObjC其他链接器标志添加到我的测试目标来解决我的问题。
#1
0
This looks good and should work. I can only think of two reasons for the behaviour you observe.
这看起来不错,应该有效。我只能想到你观察到的行为有两个原因。
If the DataCapture
class has an instance method that is also called trackEvent:
then the mock will mock the instance method and not the class method. In such cases it is necessary to wrap the method invocation in a ClassMethod()
macro, i.e. OCMExpect(ClassMethod(...)))
.
如果DataCapture类有一个也称为trackEvent的实例方法:那么mock将模拟实例方法而不是类方法。在这种情况下,有必要将方法调用包装在ClassMethod()宏中,即OCMExpect(ClassMethod(...)))。
If that's not the case my next guess would be an issue with CocoaPods. I've seen several similar reports and in the end it turned out that CocoaPods would somehow add the same binary twice. In such cases it's possible that OCMock adds its magic to one version of the class but the test uses another. I don't have a reference handy but this shouldn't be too hard to find.
如果情况并非如此,我的下一个猜测将是CocoaPods的问题。我已经看过几个类似的报告,最后发现CocoaPods会以某种方式两次添加相同的二进制文件。在这种情况下,OCMock可能会将其魔力添加到类的一个版本中,但测试使用另一个版本。我没有参考方便,但这不应该太难找到。
#2
0
Just incase anyone runs across a similar issue I was able to solve my problem by adding the -bind_at_load -ObjC
other linker flags to my test target.
只是因为任何人遇到类似问题我都能通过将-bind_at_load -ObjC其他链接器标志添加到我的测试目标来解决我的问题。