OCMock test fails - expected method was not invoked

梦想与她 提交于 2020-01-14 06:37:11

问题


IN test class

-(void)testMyTest
{
MasterViewController* masterVC = [[MasterViewController alloc]init];//[OCMockObject mockForClass:[MasterViewController class]];
id master = [OCMockObject mockForClass:[DetailViewController class]];
 [[master expect] getStringVal:@"PARAM"];
[masterVC doSomething];
 [master verify];
}

IN detailViewController

-(NSString*)getStringVal:(NSString*)param
{
NSString *returnParam = [NSString stringWithFormat:@"%@-String",param];
return returnParam;
}

IN Master view controller

  -(void)doSomething
 {
DetailViewController *detail = [[DetailViewController alloc]init];

[detail getStringVal:@"PARAM"];
NSString * returnVal = [detail getStringVal:@"PARAM2"];
NSLog(@"returnVal %@",returnVal);
NSLog(@"doSomething");
}

When I run the tests I get the following error:

doSomething Unknown.m:0: error: -[iOS5ExampleTests testMyTest] : OCMockObject[DetailViewController]: expected method was not invoked: getStringVal:@"PARAM"

It looks as if the method was not called. But if I set a breakpoint the the method, it stops and shows me that the line was executed by the application and I am also getting the logs properly.


回答1:


getStringVal method gets invoked on the real object, as you have allocated new instance of DetailViewController inside doSomething method, it is not getting called on mocked object.

Instead you can do some modification in the doSomething method

-(void)doSomething:(DetailViewController *)detail
{
     [detail getStringVal:@"PARAM"];
     NSString * returnVal = [detail getStringVal:@"PARAM2"];
     NSLog(@"returnVal %@",returnVal);
     NSLog(@"doSomething");
}

And in your test case

-(void)testMyTest
{
    MasterViewController* masterVC = [[MasterViewController alloc]init];//[OCMockObject mockForClass:[MasterViewController class]];
    id master = [OCMockObject mockForClass:[DetailViewController class]];
    [[master expect] getStringVal:@"PARAM"];
    [[master expect] getStringVal:@"PARAM2"];
    [masterVC doSomething:master];
    [master verify];
}

This should work.




回答2:


Colud it be because your Master mock object is using its own details view controller instead of your mock details view controller?

-(void)testMyTest
{
MasterViewController* masterVC = [[MasterViewController alloc]init];//[OCMockObject mockForClass:[MasterViewController class]];
id detailVC = [OCMockObject mockForClass:[DetailViewController class]];

// This is the missing line. If you don't have such a property use setValue:forKey:
masterVC.detailViewController = detailVC

 [[master expect] getStringVal:@"PARAM"];
[masterVC doSomething];
 [master verify];
}


-(void)doSomething
{
    // This is the offending line: you should be using the mock object not a real one.
    DetailViewController *detail = [[DetailViewController alloc]init];
    [detail getStringVal:@"PARAM"];
    NSString * returnVal = [detail getStringVal:@"PARAM2"];
    NSLog(@"returnVal %@",returnVal);
    NSLog(@"doSomething");
}


来源:https://stackoverflow.com/questions/16412472/ocmock-test-fails-expected-method-was-not-invoked

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!