0

Im new to unit testing and OCMock so this might be an obvious answer, just didn't find answer on google. I am trying to test a model object's method. the method has the following code:

 //takes a filepath and a pk, sets the filepath to the
BoxAppDelegate *delegate = [[UIApplication sharedApplication] delegate];
NSNumber *ifExistIndexInJson = [BoxJsonDataHelper pkExistInCurrentJson:[[[self.downloadQueue objectAtIndex:0] objectForKey:@"pk"] integerValue]];
if (ifExistIndexInJson)
{
    [[[self.downloadQueue objectAtIndex:0] objectForKey:@"fields"] setObject:path forKey:@"content"];
    NSError *error = nil;
    [[NSFileManager defaultManager] removeItemAtPath:[[[delegate.currentJsonData objectAtIndex:[ifExistIndexInJson integerValue]] objectForKey:@"fields"] objectForKey:@"content"] error:&error];
    [delegate.currentJsonData removeObjectAtIndex:[ifExistIndexInJson integerValue]];
    [delegate.currentJsonData addObject:[self.downloadQueue objectAtIndex:0]];
    [self.downloadQueue removeObjectAtIndex:0];
    if ([self.downloadQueue count] > 0)
    {
    [BoxServerRequestsObject downloadFileForPK:[[[self.downloadQueue objectAtIndex:0] objectForKey:@"pk"] integerValue]sender:self];
    }
    else
    {
        //end the progress or whatever
    }
}
else
{
    [[[self.downloadQueue objectAtIndex:0] objectForKey:@"fields"] setObject:path forKey:@"content"];
    [delegate.currentJsonData addObject:[self.downloadQueue objectAtIndex:0]];
    [self.downloadQueue removeObjectAtIndex:0];
    if ([self.downloadQueue count] > 0)
    {
    [BoxServerRequestsObject downloadFileForPK:[[[self.downloadQueue objectAtIndex:0] objectForKey:@"pk"] integerValue]sender:self];
    }
    else
    {
        //end the progress or whatever
    }

}

I need help with a couple of things:

  1. when I call [BoxJsonDataHelper pkExistInCurrentJson:...]. BoxJsonDataHelper is actually self, only it's a class method not an instance, so I call it by name, How can I fake the results of the return value so theres no dependency?

  2. How to fake a file at a path for the program to remove? than how do I check that it was removed?

  3. how do I mock BoxServerRequestObject to make the method call the mock object instead of the real one? and than how do I check if it has been called(also a class method)

My knowledge in unit testing is limited, and I have just started with OCMock and read some examples so I would appreciate full answers :)

Yoav Schwartz
  • 2,017
  • 2
  • 23
  • 43

1 Answers1

0
  1. You can mock class methods just like instance methods. They stay mocked until the mock is dealloc'ed.

    id boxJsonDataHelperMock = [OCMockObject mockForClass:BoxJsonDataHelper.class]; [[[boxJsonDataHelperMock stub] andReturn:@(1)] pkExistInCurrentJson:OCMOCK_ANY]

  2. Are you just testing whether NSFileManager works at that point? With data objects, I prefer to do the actual writing. Why not just assert that the file doesn't exist after it is removed? If you wanted to mock, you should mock "defaultManager" on NSFileManager and return a mock object that expects removeItemAtPath:error:

  3. Place a mock object in your download queue at index 0.

Ben Flynn
  • 18,524
  • 20
  • 97
  • 142
  • I'm nit-picking but they stay mocked until you call -stopMocking or the mock is dealloc'd. It is very important that you call stopMocking as soon as you're finished with the test if you mock methods of framework classes, like NSURLConnection for example. – ImHuntingWabbits Dec 26 '13 at 07:18