我对OCMock框架iOS有个问题。我实际上是在模拟UIAlertView的initWithTitle:message:delegate.方法,下面的示例无法工作,因为当我调用initWithTitle方法时,不返回带有存根的返回值。
UIAlertView *emptyAlert = [UIAlertView new];
id mockAlert = [OCMockObject partialMockForObject:[UIAlertView alloc]];
[[[mockAlert stub] andReturn:emptyAlert] initWithTitle:OCMOCK_ANY message:OCMOCK_ANY delegate:nil cancelButtonTitle:OCMOCK_ANY otherButtonTitles:nil];
UIAlertView *testAlertReturnValue = [[UIAlertView alloc] initWithTitle:@"title" message:@"message" delegate:nil cancelButtonTitle:@"ok" otherButtonTitles:nil];
if(testAlertReturnValue == emptyAlert) {
NSLog(@"UIAlertView test worked");
}但是,如果我在NSDictionary中使用相同的想法,它就会起作用。
NSDictionary *emptyDictionary = [NSDictionary new];
id mockDictionary = [OCMockObject partialMockForObject:[NSDictionary alloc]];
[[[mockDictionary stub] andReturn:emptyDictionary] initWithContentsOfFile:OCMOCK_ANY];
NSDictionary *testDictionaryReturnValue = [[NSDictionary alloc] initWithContentsOfFile:@"test"];
if(testDictionaryReturnValue == emptyDictionary) {
NSLog(@"NSDictionary test worked");
}我注意到的一件事是,"OCPartialMockObject.m“中的”OCPartialMockObject.m“方法是在NSDictionary initWithContentsOfFile调用期间调用的,而不是在UIAlertView initWithTitle调用期间调用的。
这可能是OCMock的错误吗?
发布于 2013-11-18 04:25:11
我对嘲弄UIAlertView也有异议,我最好的猜测是,是vararg把它扔出去了(但不能100%记得)。我的解决方案是为UIAlertView创建一个工厂方法,并将其作为一个类别添加。
+ (instancetype)alertViewWithTitle:(NSString *)title message:(NSString *)message delegate:(id)delegate cancelButtonTitle:(NSString *)cancelButtonTitle otherButtonTitles:(NSArray *)otherButtonTitles;注意,我用一个NSArray替换了varargs。这个方法绝对是可以模拟的,而且语法非常类似,因为我们有数组文本:
[UIAlertView alertViewWithTitle:@"Warning" message:@"Really delete your save file?" delegate:self cancelButtonTitle:@"No" otherButtonTitles:@[ @"Yes", @"Maybe" ]];如果您有修改源代码的灵活性,这将是我的建议。
编辑
仔细观察您的代码,您将创建一个部分模拟,它是init方法,然后不对它做任何操作。如果用您创建的模拟替换[UIAlertView alloc],那么您所做的工作方式可能是实际工作的。不能肯定,因为我记得我对它有意见。
发布于 2014-01-16 21:48:10
下面是一个最近的例子,OCMock现在支持类模拟。
id mockAlertView = [OCMockObject mockForClass:[UIAlertView class]];
[[[mockAlertView stub] andReturn:mockAlertView] alloc];
(void)[[[mockAlertView expect] andReturn:mockAlertView]
initWithTitle:@"Title"
message:@"Message"
delegate:OCMOCK_ANY
cancelButtonTitle:OCMOCK_ANY
otherButtonTitles:OCMOCK_ANY, nil];
[[mockAlertView expect] show];
// code that will display the alert here
[mockAlertView verify];
[mockAlertView stopMocking];从某个东西的回调中触发警报是很常见的。等待的一种方法是使用verifyWithDelay,请参阅https://github.com/erikdoe/ocmock/pull/59。
发布于 2013-12-21 19:13:31
由于某些原因,在UIAlertView中模拟+(Id)分配似乎不起作用,所以我现在使用以下修复,而不是部分模拟UIAlertView和存根(例如) initWithTitle:方法。希望这对任何面临类似问题的人都有帮助。
XCTest_UIAlertView+MyCustomCategory.m
/**
Tests alert displays on screen with correct message
Method: +(void)showAlertWithMessage:
*/
-(void)test_showAlertWithMessage
{
NSString *alertMessage = @"hello";
UIAlertView *alert = [UIAlertView new];
[UIAlertView setOCMock_UIAlertView:alert];
id alertToTest = [OCMockObject partialMockForObject:alert];
[[alertToTest expect] show];
[UIAlertView showAlertWithMessage:alertMessage];
[alertToTest verify];
XCTAssert([alert.message isEqualToString:alertMessage], @"alert message incorrect, expected [%@]", alertMessage);
}UIAlertView+MyCustomCategory.m
/**
@warning variable for unit testing only
*/
static UIAlertView *__OCMock_UIAlertView;
@implementation UIAlertView (MyCustomCategory)
+(void)setOCMock_UIAlertView:(UIAlertView *)alert
{
__OCMock_UIAlertView = alert;
}
-(id)init
{
if(__OCMock_UIAlertView)
{
self = __OCMock_UIAlertView;
if(self) {
}
return self;
}
return [super init];
}
+(void)showAlertWithMessage:(NSString *)message
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil
message:message
delegate:nil
cancelButtonTitle:@"ok"
otherButtonTitles:nil];
[alert show];
}https://stackoverflow.com/questions/20037834
复制相似问题